Reputation: 33
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);)
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"
Upvotes: 0
Views: 517
Reputation: 1526
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'. Code is fine otherwise.
So you need a respective change in your xml
file.
EDIT
You can put the following code in your ListView
EDIT II
android:id="@android:id/list"
Try calling the following after setContentView()
ListView listview = getListView();
Upvotes: 1
Reputation: 18923
If you are using ListActivity then in your xml file just change this for ListView id
android:id="@android:id/list"
and in your Activity call this
ListView lv = getListView();
If you are using Activity then just find id of your ListView in your java file by
Listview mListView = (ListView) findViewById(R.id.myListView);
and set adapter for that
mListView.setAdapter(mAdapter);
Upvotes: 0
Reputation: 6246
Make a reference to a ListView in your layout
Listview mListView = (ListView) findViewById(R.id.myListView);
Then call things like
mListView.setAdapter(mAdapter);
Upvotes: 1