package com.example.testadapter;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] strs = {"1","2","3","4","5"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strs);
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
为什么这段代码不能运行呢?LogCat提示的错误如下:
09-25 10:26:13.273: E/AndroidRuntime(1371): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testadapter/com.example.testadapter.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
望高人指点下,谢谢!
------解决思路----------------------
Your?content?must?have?a?ListView?whose?id?attribute?is?'android.R.id.list'
看下英文不就很直接了么,你的ListView的id属性必须是android.R.id.list
我猜你布局文件中肯定自己写了一个listView吧,你继承了ListActivity的话它自带了一个ListView了,不需要你写了,你要写的话就必须把id设置成提示的那样