我在创建ListView时遇到此错误。
<ListView
android:id="@+id/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
public class MainActivity extends ListActivity {
ListView l;
String[] names = {"aaa","bbb","ccc"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (ListView)findViewById(R.id.list_view);
ArrayAdapter ad = new ArrayAdapter(this,android.R.layout.activity_list_item,names);
l.setAdapter(ad);
}
}
答案 0 :(得分:1)
只需注释掉代码的这一部分:
setContentView(R.layout.activity_main);
然后替换:
l = (ListView)findViewById(R.id.list_view);
ArrayAdapter ad = new ArrayAdapter(this,android.R.layout.activity_list_item,names);
l.setAdapter(ad);
使用:
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, names));
或者您可以保留代码,只需将ListActivity更改为Activity
即可