2014-02-06 86 views
0

我很抱歉,如果這個錯誤/問題太重要了。我已經零經驗開發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 
+0

你在哪裏創建ArrayList 項目? –

回答

6

你永遠不會初始化變量items,這使你得到一個空指針異常,當您嘗試設置適配器,因爲項目是NUL湖在這裏logcat中

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_todo); 
// initialize 
     items = new ArrayList<String>(); 
// add content 
     items.add("First Item"); 
     items.add("Second Item"); 

     lvItems = (ListView) findViewById(R.id.lvItems); 
     itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); 
     lvItems.setAdapter(itemsAdapter); 
    } 

你可以看到錯誤:

做到這一點

... 
Caused by: java.lang.NullPointerException 02-06 16:26:55.038: E/AndroidRuntime(17822): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330) 02-06 16:26:55.038: E/AndroidRuntime(17822): at android.widget.ListView.setAdapter(ListView.java:470) 02-06 16:26:55.038: E/AndroidRuntime(17822): at com.example.simpletodo.TodoActivity.onCreate(TodoActivity.java:22) 02-06 .... 

這就告訴你,在NullPointerException發生TodoActivity線22

+1

這是正確的答案。我知道這很愚蠢。謝謝。我應該知道你必須實例化,然後才能分配。 – SuperNoob

+0

Nah,更改語言時容易出錯。他們都非常不同。 –

+1

重要的是要拿走你的日誌貓。 IT會告訴你很多! –

0

我們不能知道的問題,直到您添加日誌的貓,我卻發現了以下內容:

您添加項目到數組有什麼事之前他們,解決它像這樣

items.add("First Item"); 
    items.add("Second Item"); 
    itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); 
    lvItems.setAdapter(itemsAdapter); 
+0

這不是問題。可能暗戀是由於沒有創建列表而造成的。 (new ArrayList ()) –

-1

你的問題in ArrrayAdapter

items.add("First Item"); 
items.add("Second Item"); 
itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); 
lvItems.setAdapter(itemsAdapter); 
相關問題