2012-05-31 94 views
-1

我的應用程序中出現以下錯誤,我認爲它與我做佈局文件的方式有關,或者在我的代碼中使用它們。CursorAdapter的bindView()方法中的NullPointerException異常

05-31 13:39:59.283: E/AndroidRuntime(332): Uncaught handler: thread main exiting due to uncaught exception 
05-31 13:39:59.293: E/AndroidRuntime(332): java.lang.NullPointerException 
05-31 13:39:59.293: E/AndroidRuntime(332): at com.dd.qsg.ShowAchievements$AchievementAdapter.bindView(ShowAchievements.java:38) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.widget.CursorAdapter.getView(CursorAdapter.java:186) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.widget.AbsListView.obtainView(AbsListView.java:1274) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.widget.ListView.measureHeightOfChildren(ListView.java:1147) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.widget.ListView.onMeasure(ListView.java:1060) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.view.View.measure(View.java:7964) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:619) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.widget.LinearLayout.onMeasure(LinearLayout.java:280) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.view.View.measure(View.java:7964) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.view.View.measure(View.java:7964) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.widget.LinearLayout.measureVertical(LinearLayout.java:464) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.view.View.measure(View.java:7964) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.view.View.measure(View.java:7964) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.view.ViewRoot.performTraversals(ViewRoot.java:763) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.os.Looper.loop(Looper.java:123) 
05-31 13:39:59.293: E/AndroidRuntime(332): at android.app.ActivityThread.main(ActivityThread.java:4363) 
05-31 13:39:59.293: E/AndroidRuntime(332): at java.lang.reflect.Method.invokeNative(Native Method) 
05-31 13:39:59.293: E/AndroidRuntime(332): at java.lang.reflect.Method.invoke(Method.java:521) 
05-31 13:39:59.293: E/AndroidRuntime(332): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
05-31 13:39:59.293: E/AndroidRuntime(332): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
05-31 13:39:59.293: E/AndroidRuntime(332): at dalvik.system.NativeStart.main(Native Method) 

這裏是ShowAchievements類:

package com.dd.qsg; 

import android.app.ListActivity; 
import android.content.Context; 
import android.database.Cursor; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.CursorAdapter; 
import android.widget.TextView; 

public class ShowAchievements extends ListActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.achievements); 

     DatabaseHandler db = new DatabaseHandler(this); 
     Cursor cursor = db.getAchievements(this); 
     AchievementAdapter cursorAdapter = new AchievementAdapter(this, cursor); 

     this.setListAdapter(cursorAdapter); 
    } 

    private class AchievementAdapter extends CursorAdapter { 
     public AchievementAdapter(Context context, Cursor c) { 
      super(context, c); 
     } 

     @Override 
     public void bindView(View v, Context context, Cursor cursor) { 
      if(cursor.getString(cursor.getColumnIndex("completed")).equals("yes")) { 
       TextView tv = (TextView) v.findViewById(R.id.achView1); 
       tv.setTextColor(Color.GREEN); 
      } 
     } 

     @Override 
     public View newView(Context context, Cursor cursor, ViewGroup parent) { 
      LayoutInflater inflater = LayoutInflater.from(context); 
      View v = inflater.inflate(R.layout.achievements, parent, false); 
      return v; 
     } 
    }  
} 

這裏是getAchievements()方法是我DatabaseHandler類中:

public Cursor getAchievements(Context context) { 
    String selectQuery = "SELECT * FROM achievements ORDER BY _id asc"; 
    SQLiteDatabase db = this.getWritableDatabase(); 
    return db.rawQuery(selectQuery, null); 
} 

這裏是我的achievements佈局:

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

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/android:empty" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text=""/> 

</LinearLayout> 

這裏是我的achievements_item佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:id="@+id/achView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" /> 

</LinearLayout> 

我假定這些錯誤由我該怎麼做我的xml文件造成的。我是否應該將achView1TextView放在achievements佈局內,而不是achievements_item佈局?我應該從achievements刪除空的TextView

+0

我還不知道爲什麼,但在第二個函數中的'AchievementAdapter'中的'View v'似乎不包含'achView1'。 – rekire

回答

6

v爲null,並且tv is also null ......所以空指針exeption ......由於佈局文件行我的意思是item.you應該使用R.layout.achievements_item.xml而膨脹的項目,在newView方法的錯誤XML refrence ...

View v = inflater.inflate(R.layout.achievements_item, parent, false); 
            ^^^^^^^^^^^^^^^^^ 

     @Override 
     public View newView(Context context, Cursor cursor, ViewGroup parent) { 
      LayoutInflater inflater = LayoutInflater.from(context); 
      View v = inflater.inflate(R.layout.achievements_item, parent, false); 
      return v; 
     } 
+0

謝謝! :)。我還有一個問題 - 我有另一個列表視圖和它的行中的文本更大(im asssuming,因爲它使用一些沒有textview的默認值),我怎麼能使行在我的cursoradapter生成的列表視圖中相同的大小? – scarhand

+1

http://samir-mangroliya.blogspot.in/p/android-customized-listview.html –

相關問題