我正在尝试创建一个 ListView,每行显示 2 个 TextView。
但是,我在打开 Activity 时不断收到 NullPointerException,但我不知道为什么(logCat 日志没有告诉我到底是哪一行造成了问题)。
以下是相关代码:
主类EventViewActivity
public class EventViewActivity extends ListActivity {
public String[] eventTitles;
@Override
public void onCreate(Bundle savedInstanceState) {
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.eventview_layout);
eventTitles = new String[]{
getResources().getString(R.string.eventtitle1),
getResources().getString(R.string.eventtitle2),
getResources().getString(R.string.eventtitle3),
getResources().getString(R.string.eventtitle4),
getResources().getString(R.string.eventtitle5)
};
setListAdapter(new ArrayAdapter<String>(this, R.layout.eventview_layout, R.id.row_title, eventTitles));
setListAdapter(new ArrayAdapter<String>(this, R.layout.eventview_layout, R.id.row_descr, eventTitles));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(), "You have clicked an item", Toast.LENGTH_SHORT).show();
}
}
layout.xml 是:
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView android:id="@+id/android:eventlist" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/eventview_background"/>
</LinearLayout>
谢谢!