當我點擊我的ListView上的ListItems
使用Intents
時,我正嘗試切換到新頁面。不過,我不斷收到錯誤:用Intents打開新頁面Android
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
現在是一個適當的時間來學習如何使用調試器! –
@RickFalck你會知道調試器教程的好資源嗎? – Liondancer
不是。我從以前的語言/平臺調試工具的經驗使我很容易看到下一個如何工作。它們都有相同的基本方式來設置斷點,並在斷點處停下時查看變量。 Android有LogCat,這很像使用System.out。在java控制檯程序中使用println()來輸出數據而不是跟蹤它。 –