2012-01-31 67 views
0

這些錯誤出現,當用戶登錄,然後在接下來的活動中ListView需要從數據庫中讀取所有標題和顯示它們:麻煩與Android的ListView

的logcat:

01-30 12:52:57.405: E/AndroidRuntime(2905): FATAL EXCEPTION: main 
01-30 12:52:57.405: E/AndroidRuntime(2905): java.lang.NullPointerException 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.widget.AbsListView.obtainView(AbsListView.java:1294) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1198) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.widget.ListView.onMeasure(ListView.java:1109) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.view.View.measure(View.java:8171) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:563) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:378) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.view.View.measure(View.java:8171) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.view.View.measure(View.java:8171) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:526) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.view.View.measure(View.java:8171) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.view.View.measure(View.java:8171) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.view.ViewRoot.performTraversals(ViewRoot.java:801) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.os.Looper.loop(Looper.java:123) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at java.lang.reflect.Method.invoke(Method.java:521) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
01-30 12:52:57.405: E/AndroidRuntime(2905):  at dalvik.system.NativeStart.main(Native Method) 

我的問題是在這裏某處:

package com.todo; 

import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 

public class Todo extends ListActivity implements OnClickListener{ 
    private Button add,delete,search,exit; 
    private Intent intent; 
    private ListView listView; 
    DatabaseHelper db; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     db=new DatabaseHelper(this); 
     db.open(); 
     setupViews(); 
     db.close(); 

    } 
    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.main_add_btn: 
      //open activity to add item 
      intent=new Intent(this, InsertItem.class); 
      startActivity(intent); 

      break; 
     case R.id.main_delete_btn: 
      //delete item 
      break; 
     case R.id.main_exit_btn: 
      finish(); 
      break; 

     } 

    } 
    private void setupViews(){ 
     add=(Button) findViewById(R.id.main_add_btn); 
     delete=(Button) findViewById(R.id.main_delete_btn); 
     search=(Button) findViewById(R.id.main_search_btn); 
     exit=(Button) findViewById(R.id.main_exit_btn); 
     add.setOnClickListener(this); 
     delete.setOnClickListener(this); 
     search.setOnClickListener(this); 
     exit.setOnClickListener(this); 
     listView=getListView(); 
//  int l=(int) intent.getLongExtra("userID", -1);  
     Log.v("test","tuja"); 
     ; 
     ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,db.GetAllItems(1)); 
//  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
//    android.R.layout.simple_list_item_1, db.GetAllItems(1)); 
     Log.v("test","tuja2"); 
     setListAdapter(adapter); 

    } 
} 

和GetAllItems功能:

public String[] GetAllItems(int userID) { 
     Cursor cur = database.query(true, "todoItems", 
       new String[] { "title" }, "userID" + "=" + userID, null, null, 
       null, null, null); 
     int i; 
     String arr[] = new String[40]; 
     i = 0; 

     cur.moveToFirst(); 
     while (cur.isAfterLast() == false) { 
      arr[i] = cur.getString(cur.getColumnIndex("title")); 
      Log.v("test", arr[i]); 
      i++; 
      cur.moveToNext(); 
     } 
     return arr; 
    } 

我試過了,但我找不到我錯在哪裏。

+0

@Natali已解決請參閱下面的解決方案 – Darko 2012-01-31 12:57:49

回答

0

刪除代碼setContentView(R.layout.main);中的這一行,然後運行。

+0

問題是數組的長度爲40,而且listview顯示的空值不能顯示,那就是錯誤:)所以我用cur.getCoung()計算了行數。解決方案是'String arr [] = new String [cur.getCount()];' – Darko 2012-01-31 12:54:10

+0

在您的類代碼中,您擴展了ListActivity。由於setcontentview,我也有Null指針異常。一旦我刪除它工作正常。這就是爲什麼我建議。 – Aerrow 2012-01-31 12:59:14