为啥下面的这个小Demo会报空指针异常。。
先看布局文件main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ListView android:id="@+id/listview1" android:layout_width="fill_parent" android:layout_height="wrap_content" ></ListView> </LinearLayout>
public class AndroidListViewActivity extends Activity { private ListView listView ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); listView = (ListView)findViewById(R.id.listview1); listView.setAdapter(new ArrayAdapter<String>(AndroidListViewActivity.this,android.R.layout.simple_list_item_1, new String[]{"测试数据1","测试数据2","测试数据3","测试数据4","测试数据5","测试数据6","测试数据7"})); setContentView(R.layout.main); } }