2013-12-24 63 views
0

我是新來的Android編程和我似乎無法找到與下面的程序問題:我如何獲得這個簡單的筆記應用程序工作?

主要類:

public class MainActivity extends Activity { 

    ListView listView; 
    ArrayList<String> listItems = new ArrayList<String>(); 
    ArrayAdapter<String> adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     listView = (ListView) findViewById(R.id.listView1); 
     adapter = new ArrayAdapter<String>(this, R.layout.my_list_layout, 
       listItems); 
     listView.setAdapter(adapter); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 

     case R.id.menu_insert: 

      createReminder(); 
      return true; 

     } 
     return super.onOptionsItemSelected(item); 
    } 

    private static final int ACTIVITY_CREATE = 0; 

    private void createReminder() { 

     Intent intent = new Intent(this, ReminderEditActivity.class); 
     startActivityForResult(intent, ACTIVITY_CREATE); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, 
      Intent intent) { 

     if (resultCode == RESULT_OK) { 

      listItems.add(intent.getStringExtra("reminder_text")); 
      adapter.notifyDataSetChanged(); 
     } 

    } 

} 

二級類:

public class ReminderEditActivity extends Activity { 

    EditText editText; 

    public void onCreate(Bundle b) { 

     super.onCreate(b); 
     setContentView(R.layout.reminder_edit); 

     editText = (EditText) findViewById(R.id.editText1); 
    } 

    public void onPostButtonClick(View view) { 

     Intent intent = new Intent(); 
     intent.putExtra("text", editText.getText().toString()); 
     setResult(Activity.RESULT_OK, intent); 
     finish(); 
    } 
} 

所以程序啓動正常,當我點擊添加按鈕時,我會帶着文本框和按鈕進入第二頁 。當我填寫字段並單擊按鈕時,程序崩潰。我得到一個空指針異常,但它並沒有指向我的代碼中的任何地方,只是指向陣列適配器代碼(更具體地說是ArrayAdapter.java:394)。我有一些編程經驗,但我新來調試android項目。任何方向將不勝感激。

堆棧跟蹤:

12-23 18:08:37.881: D/dalvikvm(1654): GC_FOR_ALLOC freed 71K, 5% free 2989K/3136K, paused 40ms, total 43ms 
12-23 18:08:37.891: I/dalvikvm-heap(1654): Grow heap (frag case) to 3.596MB for 635812-byte allocation 
12-23 18:08:37.951: D/dalvikvm(1654): GC_FOR_ALLOC freed 3K, 5% free 3606K/3760K, paused 56ms, total 56ms 
12-23 18:08:38.251: D/gralloc_goldfish(1654): Emulator without GPU emulation detected. 
12-23 18:09:07.971: I/Choreographer(1654): Skipped 82 frames! The application may be doing too much work on its main thread. 
12-23 18:09:08.151: I/Choreographer(1654): Skipped 39 frames! The application may be doing too much work on its main thread. 
12-23 18:09:10.231: I/Choreographer(1654): Skipped 33 frames! The application may be doing too much work on its main thread. 
12-23 18:09:17.151: D/AndroidRuntime(1654): Shutting down VM 
12-23 18:09:17.151: W/dalvikvm(1654): threadid=1: thread exiting with uncaught exception (group=0xb3a19b90) 
12-23 18:09:17.261: E/AndroidRuntime(1654): FATAL EXCEPTION: main 
12-23 18:09:17.261: E/AndroidRuntime(1654): Process: food.start.thefoodapp, PID: 1654 
12-23 18:09:17.261: E/AndroidRuntime(1654): java.lang.NullPointerException 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.widget.AbsListView.obtainView(AbsListView.java:2263) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1263) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.widget.ListView.onMeasure(ListView.java:1175) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.View.measure(View.java:16458) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.View.measure(View.java:16458) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.View.measure(View.java:16458) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.View.measure(View.java:16458) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.View.measure(View.java:16458) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.Choreographer.doCallbacks(Choreographer.java:562) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.Choreographer.doFrame(Choreographer.java:532) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.os.Handler.handleCallback(Handler.java:733) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.os.Handler.dispatchMessage(Handler.java:95) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.os.Looper.loop(Looper.java:137) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at java.lang.reflect.Method.invoke(Method.java:515) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
12-23 18:09:17.261: E/AndroidRuntime(1654):  at dalvik.system.NativeStart.main(Native Method) 
12-23 18:09:21.121: I/Process(1654): Sending signal. PID: 1654 SIG: 9 

這裏是my_list_layout.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/identView" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 

</TextView> 
+0

請添加完整的堆棧跟蹤。 –

回答

1

看到這個答案(android - ArrayAdapter.createViewFromResource(int, View, ViewGroup, int) line: 355 NullPointerException)。

它可能與您的my_list_layout有關,以及Adapter如何嘗試將給定的String值填充到它中。特別是,一個ArrayAdapter文檔指出:

默認情況下,這個類預期所提供的資源ID引用 一個TextView的

如果這不是所需的行爲,然後修改您的佈局或創建ArrayAdapter的子類並重寫getView()方法。

ArrayAdapter文檔:http://developer.android.com/reference/android/widget/ArrayAdapter.html

編輯:至於什麼佈局或擴展ArrayAdapter貌似更多的細節,佈局應該是這個樣子,用的TextView在佈局的根。

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

在另一方面,如果你想擴展ArrayAdapter(這是很常見的),你可以在谷歌看看爲一些教程,或在本教程中看看第3.2節:

http://www.vogella.com/articles/AndroidListView/article.html

一切都好!

+0

你能詳細說明我將如何修改我的佈局或ArrayAdapter的子類應該是什麼樣子? –

相關問題