当我使用ListItems
点击ListView上的Intents
之一时,我正在尝试切换到新页面。但是,我一直收到错误:
Unfortunately, ToDoApp has stopped
我不太确定如何使用调试器,但这是我到目前为止所做的。
public class ToDoActivity extends Activity {
private ArrayList<String> todoItems;
private ArrayAdapter<String> todoAdapter; // declare array adapter which will translate the piece of data to teh view
private ListView lvItems; // attach to lsitview
private EditText etNewItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do);
etNewItem = (EditText) findViewById(R.id.etNewItem);
lvItems = (ListView) findViewById(R.id.lvItems); // now we have access to ListView
//populateArrayItems(); // call function
readItems(); // read items from file
todoAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems); //create adapter
lvItems.setAdapter(todoAdapter); // populate listview using the adapter
//todoAdapter.add("item 4");
setupListViewListener();
setupEditItemListener();
}
private void launchEditItem() {
Intent i = new Intent(this, EditItemActivity.class);
startActivity(i);
}
private void setupEditItemListener() { // on click, run this function to display edit page
lvItems.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
launchEditItem();
}
});
}
private void setupListViewListener() {
lvItems.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapter, View item, int pos, long id) {
todoItems.remove(pos);
todoAdapter.notifyDataSetChanged(); // has adapter look back at the array list and refresh it's data and repopulate the view
writeItems();
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.to_do, menu);
return true;
}
public void onAddedItem(View v) {
String itemText = etNewItem.getText().toString();
todoAdapter.add(itemText); // add to adapter
etNewItem.setText(""); //clear edit text
writeItems(); //each time to add item, you want to write to file to memorize
}
private void readItems() {
File filesDir = getFilesDir(); //return path where files can be created for android
File todoFile = new File(filesDir, "todo.txt");
try {
todoItems = new ArrayList<String>(FileUtils.readLines(todoFile)); //populate with read
}catch (IOException e) { // if files doesn't exist
todoItems = new ArrayList<String>();
}
}
private void writeItems() {
File filesDir = getFilesDir(); //return path where files can be created for android
File todoFile = new File(filesDir, "todo.txt");
try {
FileUtils.writeLines(todoFile, todoItems); // pass todoItems to todoFile
} catch (IOException e) {
e.printStackTrace();
}
}
}
我认为运行setupEditItemListener()
函数会打开我的新Activity
,但它不会这样做。有人可以帮我指出我哪里出错了吗?谢谢!
控制台输出:
[2013-12-07 20:39:05 - ToDoApp] ------------------------------
[2013-12-07 20:39:05 - ToDoApp] Android Launch!
[2013-12-07 20:39:05 - ToDoApp] adb is running normally.
[2013-12-07 20:39:05 - ToDoApp] Performing com.example.todoapp.ToDoActivity activity launch
[2013-12-07 20:39:05 - ToDoApp] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'NexusS'
[2013-12-07 20:39:05 - ToDoApp] Uploading ToDoApp.apk onto device 'emulator-5554'
[2013-12-07 20:39:05 - ToDoApp] Installing ToDoApp.apk...
[2013-12-07 20:39:05 - ToDoApp] Success!
[2013-12-07 20:39:05 - ToDoApp] Starting activity com.example.todoapp.ToDoActivity on device emulator-5554
[2013-12-07 20:39:06 - ToDoApp] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.todoapp/.ToDoActivity }
logcat的:
12-08 16:03:33.854: D/dalvikvm(2012): Not late-enabling CheckJNI (already on)
12-08 16:03:33.884: D/dalvikvm(2012): GC_FOR_ALLOC freed 37K, 6% free 2641K/2808K, paused 0ms, total 0ms
12-08 16:03:33.894: I/dalvikvm-heap(2012): Grow heap (frag case) to 3.307MB for 635808-byte allocation
12-08 16:03:33.914: D/dalvikvm(2012): GC_FOR_ALLOC freed 2K, 6% free 3259K/3432K, paused 19ms, total 19ms
12-08 16:03:33.924: D/AndroidRuntime(2012): Shutting down VM
12-08 16:03:33.924: W/dalvikvm(2012): threadid=1: thread exiting with uncaught exception (group=0xb2ec4648)
12-08 16:03:33.924: E/AndroidRuntime(2012): FATAL EXCEPTION: main
12-08 16:03:33.924: E/AndroidRuntime(2012): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.todoapp/com.example.todoapp.ToDoActivity}: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
12-08 16:03:33.924: E/AndroidRuntime(2012): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-08 16:03:33.924: E/AndroidRuntime(2012): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-08 16:03:33.924: E/AndroidRuntime(2012): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-08 16:03:33.924: E/AndroidRuntime(2012): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-08 16:03:33.924: E/AndroidRuntime(2012): at android.os.Handler.dispatchMessage(Handler.java:99)
12-08 16:03:33.924: E/AndroidRuntime(2012): at android.os.Looper.loop(Looper.java:137)
12-08 16:03:33.924: E/AndroidRuntime(2012): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-08 16:03:33.924: E/AndroidRuntime(2012): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 16:03:33.924: E/AndroidRuntime(2012): at java.lang.reflect.Method.invoke(Method.java:525)
12-08 16:03:33.924: E/AndroidRuntime(2012): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-08 16:03:33.924: E/AndroidRuntime(2012): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-08 16:03:33.924: E/AndroidRuntime(2012): at dalvik.system.NativeStart.main(Native Method)
12-08 16:03:33.924: E/AndroidRuntime(2012): Caused by: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
12-08 16:03:33.924: E/AndroidRuntime(2012): at android.widget.AdapterView.setOnClickListener(AdapterView.java:773)
12-08 16:03:33.924: E/AndroidRuntime(2012): at com.example.todoapp.ToDoActivity.setupEditItemListener(ToDoActivity.java:52)
12-08 16:03:33.924: E/AndroidRuntime(2012): at com.example.todoapp.ToDoActivity.onCreate(ToDoActivity.java:41)
12-08 16:03:33.924: E/AndroidRuntime(2012): at android.app.Activity.performCreate(Activity.java:5133)
12-08 16:03:33.924: E/AndroidRuntime(2012): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-08 16:03:33.924: E/AndroidRuntime(2012): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-08 16:03:33.924: E/AndroidRuntime(2012): ... 11 more
答案 0 :(得分:2)
也许考虑更换电话
setOnClickListener(..)
带
setOnItemClickListener(..)
您对这些实现的调用实现了一个匿名侦听器类,因此此更改可能需要更改侦听器接口名称,方法签名。
您可以在
的堆栈跟踪中看到建议RuntimeException:不要为AdapterView调用setOnClickListener。您可能需要setOnItemClickListener而不是
答案 1 :(得分:1)
答案只在你的logcat中。养成经历logcat的习惯: -
12-08 16:03:33.924: E/AndroidRuntime(2012): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.todoapp/com.example.todoapp.ToDoActivity}: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
使用setOnItemClick
侦听器并在其中开始您的活动
答案 2 :(得分:1)
您对列表项使用了错误的侦听器。它必须是“onItemClickListener”
private void setupEditItemListener() {
lvItems.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Do anything you want to do here...
}
});
}
答案 3 :(得分:0)
Dude,你创建了一个自定义函数,在这些函数中你编写了listview监听器。只要打电话给他们,这些听众就不会开枪。
只需在onCreate()
中编写没有自定义函数的侦听器。它会工作。此外,你的应用停止了。请养成发布堆栈跟踪以查看错误的习惯。也许它是某个行上出现的空指针异常。它通常是应用程序崩溃的原因。