我是一个初学者,正在编写一个 listView 代码,它在 OnclickListner 上执行一个操作。但它给出了错误“不幸停止”。我想对选择不同的列表项执行不同的操作。我是 android 开发的新手,所以我在线学习基本教程。
我有这个开关/案例结构:
package com.list;
import android.os.Bundle;
import android.app.ListActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
final int Aatma = 0; //should be equal to the index in your array.
final int Special26 = 1;
final int Dabangg2 = 2;
String menu[] = {"Aatma","Special26","Dabangg2" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ListView l = (ListView)findViewById(R.id.list);
setListAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,menu));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
protected void onListItemClick(ListView l, View v, int position, long id) {
// super.onListItemClick(l, v, position, id);
CheckedTextView selectedItem = (CheckedTextView) v;
boolean isChecked = selectedItem.isChecked();
Log.e("TAG","item clicked position = " + position + " isChecked = " + isChecked);
switch (position) {
case Aatma:
Toast.makeText(this, "AAtma !", Toast.LENGTH_LONG).show();
break;
case Special26:
Toast.makeText(this, "Special26 !", Toast.LENGTH_LONG).show();
break;
case Dabangg2:
Toast.makeText(this, "Dabangg 2 !", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
Error Log-
04-29 18:28:21.514: W/dalvikvm(782): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-29 18:28:21.544: E/AndroidRuntime(782): FATAL EXCEPTION: main
04-29 18:28:21.544: E/AndroidRuntime(782): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.list/com.list.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-29 18:28:21.544: E/AndroidRuntime(782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-29 18:28:21.544: E/AndroidRuntime(782): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-29 18:28:21.544: E/AndroidRuntime(782): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-29 18:28:21.544: E/AndroidRuntime(782): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-29 18:28:21.544: E/AndroidRuntime(782): at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 18:28:21.544: E/AndroidRuntime(782): at android.os.Looper.loop(Looper.java:137)
04-29 18:28:21.544: E/AndroidRuntime(782): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-29 18:28:21.544: E/AndroidRuntime(782): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 18:28:21.544: E/AndroidRuntime(782): at java.lang.reflect.Method.invoke(Method.java:511)
04-29 18:28:21.544: E/AndroidRuntime(782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-29 18:28:21.544: E/AndroidRuntime(782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-29 18:28:21.544: E/AndroidRuntime(782): at dalvik.system.NativeStart.main(Native Method)
04-29 18:28:21.544: E/AndroidRuntime(782): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-29 18:28:21.544: E/AndroidRuntime(782): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
04-29 18:28:21.544: E/AndroidRuntime(782): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
04-29 18:28:21.544: E/AndroidRuntime(782): at android.app.Activity.setContentView(Activity.java:1881)
04-29 18:28:21.544: E/AndroidRuntime(782): at com.list.MainActivity.onCreate(MainActivity.java:25)
04-29 18:28:21.544: E/AndroidRuntime(782): at android.app.Activity.performCreate(Activity.java:5104)
04-29 18:28:21.544: E/AndroidRuntime(782): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-29 18:28:21.544: E/AndroidRuntime(782): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-29 18:28:21.544: E/AndroidRuntime(782): ... 11 more