简体   繁体   English

Android - 列表活动到活动

[英]Android - ListActivity to Activity

i have this code; 我有这个代码;

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);

}

for use that i need 用于我需要的

public class MainActivity extends ListActivity {

but with this when i use 但是当我使用它的时候

setContentView(R.layout.main);

i get error (line 25 is "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

my question is who can i do some thing like "displayResultList" when i using "extends Activity" 我的问题是,当我使用“extends Activity”时,我可以做一些像“displayResultList”这样的事情

Make a reference to a ListView in your layout 在布局中引用ListView

Listview mListView = (ListView) findViewById(R.id.myListView);

Then call things like 然后称之为

mListView.setAdapter(mAdapter);

Problem is described in the following line. 问题在以下行中描述。

Your content must have a ListView whose id attribute is 'android.R.id.list'

Since you are using ListActivity your ListView view must have id in the name 'android.R.id.list'. 由于您使用的是ListActivity ListView视图必须在名称“android.R.id.list”中具有id。 Code is fine otherwise. 否则代码就好了。

So you need a respective change in your xml file. 因此,您需要对xml文件进行相应的更改。

EDIT 编辑

You can put the following code in your ListView 您可以将以下代码放在ListView

EDIT II 编辑二

android:id="@android:id/list" 

Try calling the following after setContentView() 尝试在setContentView()之后调用以下内容

ListView listview = getListView();

If you are using ListActivity then in your xml file just change this for ListView id 如果您正在使用ListActivity,那么在您的xml文件中只需为ListView id更改此项

 android:id="@android:id/list" 

and in your Activity call this 并在你的Activity中调用它

ListView lv = getListView();

If you are using Activity then just find id of your ListView in your java file by 如果你正在使用Activity,那么只需在你的java文件中找到你的ListView的id

Listview mListView = (ListView) findViewById(R.id.myListView);

and set adapter for that 并为此设置适配器

mListView.setAdapter(mAdapter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM