我的代碼中有listview
,我想對其設置adapter
。即使在初始化後,該視圖也爲空
但問題是,即使在初始化listview
之後,它仍然是null導致nullPointerException
。 (我通過日誌記錄和調試進行了檢查)
我無法從該XML訪問任何視圖。
我錯過了什麼?任何幫助讚賞。
XML
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:scrollbars="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@+id/lvToday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#FFF"
android:dividerHeight="2dp" />
<ListView
android:id="@+id/lvTomorrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/lvToday"
android:divider="#FFF"
android:dividerHeight="2dp" />
</RelativeLayout>
</ScrollView>
活動文件
public class DashboardActivity extends Activity {
ListView lvToday, lvTomorrow;
TextView lblCall;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
init();
}
private void init() {
lvToday = (ListView) findViewById(R.id.lvToday);
lvTomorrow = (ListView) findViewById(R.id.lvTomorrow);
TodayAppAdapter adapter = new TodayAppAdapter(DashboardActivity.this,
DashboardActivity.this);
// This line is giving NULL
lvToday.setAdapter(adapter);
TomorrowAppAdapter adapter1 = new TomorrowAppAdapter(
DashboardActivity.this, DashboardActivity.this);
// This line is giving NULL
lvTomorrow.setAdapter(adapter1);
}
}
檢查XML名稱是否是「儀表板」 – Chet