我很抱歉,如果這個錯誤/問題太重要了。我已經零經驗開發Android應用程序,所以我試圖去通過codepath教程之一,特別是this one,我在幻燈片11Android應用程序崩潰。不知道爲什麼
這裏是java:
包com.example.simpletodo;
package com.example.simpletodo;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TodoActivity extends Activity {
ArrayList<String> items;
ArrayAdapter<String> itemsAdapter;
ListView lvItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo);
lvItems = (ListView) findViewById(R.id.lvItems);
items.add("First Item");
items.add("Second Item");
itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.todo, menu);
return true;
}
}
下面是XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TodoActivity" >
<ListView
android:id="@+id/lvItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/etNewITem"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
<EditText
android:id="@+id/etNewITem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/lvItems"
android:layout_alignParentBottom="true"
android:ems="10"
android:hint="Enter new item" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnAddItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Add" />
</RelativeLayout>
該應用程序出現在java的行#22 lvItems.setAdapter(itemsAdapter);
任何意見理解要崩潰。我是一個PHP的傢伙,所以我不習慣詳細的代碼(學習)以及如何調試編譯的程序。謝謝!從logcat的
錯誤消息:
02-06 16:29:31.563: W/dalvikvm(18897): threadid=1: thread exiting with uncaught exception (group=0x41d39700)
02-06 16:29:31.563: E/AndroidRuntime(18897): FATAL EXCEPTION: main
02-06 16:29:31.563: E/AndroidRuntime(18897): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simpletodo/com.example.simpletodo.TodoActivity}: java.lang.NullPointerException
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread.access$700(ActivityThread.java:168)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.os.Looper.loop(Looper.java:137)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread.main(ActivityThread.java:5493)
02-06 16:29:31.563: E/AndroidRuntime(18897): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 16:29:31.563: E/AndroidRuntime(18897): at java.lang.reflect.Method.invoke(Method.java:525)
02-06 16:29:31.563: E/AndroidRuntime(18897): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
02-06 16:29:31.563: E/AndroidRuntime(18897): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
02-06 16:29:31.563: E/AndroidRuntime(18897): at dalvik.system.NativeStart.main(Native Method)
02-06 16:29:31.563: E/AndroidRuntime(18897): Caused by: java.lang.NullPointerException
02-06 16:29:31.563: E/AndroidRuntime(18897): at com.example.simpletodo.TodoActivity.onCreate(TodoActivity.java:21)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.Activity.performCreate(Activity.java:5372)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-06 16:29:31.563: E/AndroidRuntime(18897): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
02-06 16:29:31.563: E/AndroidRuntime(18897): ... 11 more
你在哪裏創建ArrayList項目? –