我有这个代码;
private void displayResultList() {
TextView tView = new TextView(this);
getListView().addHeaderView(tView);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);
}
用于我需要的用途
public class MainActivity extends ListActivity {
但是当我使用
时setContentView(R.layout.main);
我得到错误(第25行是“setContentView(R.layout.main);)
02-26 04:47:22.770: E/AndroidRuntime(1031): FATAL EXCEPTION: main
02-26 04:47:22.770: E/AndroidRuntime(1031): Process: com.example.app.arr, PID: 1031
02-26 04:47:22.770: E/AndroidRuntime(1031): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app.arr/com.example.app.arr.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-26 04:47:22.770: E/AndroidRuntime(1031): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-26 04:47:22.770: E/AndroidRuntime(1031): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-26 04:47:22.770: E/AndroidRuntime(1031): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-26 04:47:22.770: E/AndroidRuntime(1031): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-26 04:47:22.770: E/AndroidRuntime(1031): at android.os.Handler.dispatchMessage(Handler.java:102)
02-26 04:47:22.770: E/AndroidRuntime(1031): at android.os.Looper.loop(Looper.java:136)
02-26 04:47:22.770: E/AndroidRuntime(1031): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-26 04:47:22.770: E/AndroidRuntime(1031): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 04:47:22.770: E/AndroidRuntime(1031): at java.lang.reflect.Method.invoke(Method.java:515)
02-26 04:47:22.770: E/AndroidRuntime(1031): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-26 04:47:22.770: E/AndroidRuntime(1031): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-26 04:47:22.770: E/AndroidRuntime(1031): at dalvik.system.NativeStart.main(Native Method)
02-26 04:47:22.770: E/AndroidRuntime(1031): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
02-26 04:47:22.770: E/AndroidRuntime(1031): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
02-26 04:47:22.770: E/AndroidRuntime(1031): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
02-26 04:47:22.770: E/AndroidRuntime(1031): at android.app.Activity.setContentView(Activity.java:1929)
02-26 04:47:22.770: E/AndroidRuntime(1031): at com.example.app.arr.MainActivity.onCreate(MainActivity.java:25)
02-26 04:47:22.770: E/AndroidRuntime(1031): at android.app.Activity.performCreate(Activity.java:5231)
02-26 04:47:22.770: E/AndroidRuntime(1031): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-26 04:47:22.770: E/AndroidRuntime(1031): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-26 04:47:22.770: E/AndroidRuntime(1031): ... 11 more
我的问题是,当我使用“extends Activity”时,我可以做一些像“displayResultList”这样的事情
答案 0 :(得分:1)
在布局中引用ListView
Listview mListView = (ListView) findViewById(R.id.myListView);
然后调用类似
的内容mListView.setAdapter(mAdapter);
答案 1 :(得分:1)
问题在以下行中描述。
Your content must have a ListView whose id attribute is 'android.R.id.list'
由于您使用的是ListActivity
,因此ListView
视图的名称必须为'android.R.id.list'。否则代码就好了。
因此,您需要对xml
文件进行相应更改。
修改强>
您可以将以下代码放入ListView
编辑II
android:id="@android:id/list"
尝试在setContentView()
ListView listview = getListView();
答案 2 :(得分:0)
如果您使用 ListActivity ,那么在您的xml文件中,只需为ListView ID更改此内容
android:id="@android:id/list"
并在您的Activity中调用此
ListView lv = getListView();
如果您使用的是 活动 ,那么只需在您的java文件中找到您的ListView的ID
Listview mListView = (ListView) findViewById(R.id.myListView);
并为该
设置适配器mListView.setAdapter(mAdapter);