2017-02-24 53 views
-1

我試圖把ListViewTextView放在相同的佈局,我設法這樣做,但執行應用程序時,我得到了一些錯誤。 下面是XML代碼 ` 如何把ListView和TextView放在android的相同佈局中?

<ListView 
     android:id="@+id/lvCourses" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="40" > 
    </ListView> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="60" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/tvHOD" 
      android:layout_width="wrap_content" 
      android:layout_height="48dp" 
      android:text="Output here" /> 
    </LinearLayout> 

</LinearLayout> 

這裏是Java代碼

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.teacher); 
    Courses = (ListView) findViewById(R.id.lvCourses); 
    Output = (TextView) findViewById(R.id.tvHOD); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1,  Names); 
    Courses.setAdapter(adapter); 
    Courses.setOnItemClickListener(this); 
} 

@Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 

    Output.setText(HOD[arg2]); 

} 

Here is the logcat screenshot

enter image description here

logcat中說,錯誤是在的onCreate功能,但在看見碼我無法理解一切看起來很好。 需要幫助。

+2

你應該張貼您的日誌另存爲文本不作爲截圖好友 –

+0

請參考您問題頂部鏈接的副本。所需的ID具有非常特定的格式。 –

回答

2

在logcat的輸出,你可以挑出:

您的內容必須有一個ListView其id屬性爲 'android.R.id.list'

你錯過了這條線?這可能是問題所在。只是檢查出來。

所以,這是解決方案:

< ListView的機器人:ID = 「@機器人:ID /列表」>

+0

我嘗試將列表視圖的ID更改爲「列表」,但問題仍然存在 –

+0

@AshishPal顯示新日誌,可能還有另一個錯誤。 –

+0

@Ashish我已經讓我的答案更具體。看看它現在工作! – varun

相關問題