2016-01-04 65 views
1

我已經調試了一個多小時,但無法找到解決方案。我爲一個大學項目創建了一個運動隊應用程序,並且我正在讓團隊成員完成每日日記。然後團隊經理可以查看個人日記的回覆。setText方法上的NullPointerException

我遇到問題的地方是從數據庫返回信息。下面是我的logcat,它表明問題出在我的DiaryAdapter類的第58行。

01-04 16:27:05.339 23442-23442/com.example.myacer.clubhub E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.NullPointerException 
      at com.example.myacer.clubhub.Database.DiaryAdapter.bindView(DiaryAdapter.java:58) 
      at android.widget.CursorAdapter.getView(CursorAdapter.java:250) 
      at android.widget.AbsListView.obtainView(AbsListView.java:2177) 
      at android.widget.ListView.measureHeightOfChildren(ListView.java:1247) 
      at android.widget.ListView.onMeasure(ListView.java:1159) 
      at android.view.View.measure(View.java:15848) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012) 
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
      at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052) 
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:590) 
      at android.view.View.measure(View.java:15848) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012) 
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
      at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124) 
      at android.view.View.measure(View.java:15848) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012) 
      at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393) 
      at android.view.View.measure(View.java:15848) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012) 
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
      at android.view.View.measure(View.java:15848) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012) 
      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) 
      at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) 
      at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
      at android.view.View.measure(View.java:15848) 
      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012) 
      at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
      at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189) 
      at android.view.View.measure(View.java:15848) 
      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905) 
      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104) 
      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284) 
      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) 
      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481) 
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 
      at android.view.Choreographer.doCallbacks(Choreographer.java:562) 
      at android.view.Choreographer.doFrame(Choreographer.java:532) 
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 
      at android.os.Handler.handleCallback(Handler.java:730) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:5103) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:525) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
      at dalvik.system.NativeStart.main(Native Method) 

我DiaryAdapter類是如下

public class DiaryAdapter extends CursorAdapter { 

    public DiaryAdapter(Context context, Cursor cursor, int flags) { 
     super(context, cursor, 0); 
    } 

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

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 

     TextView playerName = (TextView) view.findViewById(R.id.EditTextName); 
     TextView playerSleepLength = (TextView) view.findViewById(R.id.sleepLengthHolder); 
     TextView playerSleepQuality = (TextView) view.findViewById(R.id.sleepQualityHolder); 
     TextView playerEnergy = (TextView) view.findViewById(R.id.energyHolder); 
     TextView playerMood = (TextView) view.findViewById(R.id.moodHolder); 
     TextView playerAppetite = (TextView) view.findViewById(R.id.appetiteHolder); 
     TextView playerWaterIntake = (TextView) view.findViewById(R.id.waterIntakeHolder); 
     TextView playerSoreness = (TextView) view.findViewById(R.id.sorenessHolder); 
     TextView playerWorkoutType = (TextView) view.findViewById(R.id.workoutTypeHolder); 
     TextView playerWorkoutLength = (TextView) view.findViewById(R.id.workoutLengthHolder); 
     TextView playerWorkoutRPE = (TextView) view.findViewById(R.id.workoutRPEHolder); 

     //extract properties from cursor 
     String _id = cursor.getString(cursor.getColumnIndex("_id")); 
     String memberName = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.PLAYER_NAME)); 
     String memberSleepLength = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.SLEEP_LENGTH)); 
     String memberSleepQuality = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.SLEEP_QUALITY)); 
     String memberEnergy = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.ENERGY)); 
     String memberMood = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.MOOD)); 
     String memberAppetite = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.APPETITE)); 
     String memberWaterIntake = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.WATER_INTAKE)); 
     String memberSoreness = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.SORENESS)); 
     String memberWorkoutType = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.WORKOUT_TYPE)); 
     String memberWorkoutLength = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.WORKOUT_LENGTH)); 
     String memberWorkoutRPE = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.WORKOUT_RPE)); 

     playerName.setText(memberName); 
     playerSleepLength.setText(memberSleepLength); 
     playerSleepQuality.setText(memberSleepQuality); 
     playerEnergy.setText(memberEnergy); 
     playerMood.setText(memberMood); 
     playerAppetite.setText(memberAppetite); 
     playerWaterIntake.setText(memberWaterIntake); 
     playerSoreness.setText(memberSoreness); 
     playerWorkoutType.setText(memberWorkoutType); 
     playerWorkoutLength.setText(memberWorkoutLength); 
     playerWorkoutRPE.setText(memberWorkoutRPE); 

    } 

} 

因此,有錯誤就出現在這裏:

playerName.setText(memberName); 

playerName引用的EditText EditTextName在activity_diary.xml

<?xml version="1.0" encoding="UTF-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/scrollView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center_vertical" 
    tools:context=".DiaryActivity"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <EditText 
      android:id="@+id/EditTextName" 
      android:paddingTop="5dp" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/playerName" 
      android:inputType="textPersonName" /> 

     <TextView 
      android:id="@+id/TextViewSpinner" 
      android:layout_below="@+id/EditTextName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingTop="10dp" 
      android:text="@string/sleepLength"/> 

     <Spinner 
      android:id="@+id/SpinnerSleepLength" 
      android:layout_below="@+id/TextViewSpinner" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:entries="@array/sleepLengthList" 
      android:prompt="@string/sleepLength" /> 

我竟被d非常感謝在這個問題上的另一個眼睛。提前致謝。

+1

做到這一點你真的使用activity_diary.xml佈局視圖中的適配器? – tpr

+0

您是否檢查過哪一個是null,'playerName'或'memberName'?我假設它是其中之一。 – DigitalNinja

+2

看起來你正在使用'R.layout.layout_diaryresponse',而不是'activity_diary'。你不能從適配器中引用它。 –

回答

2

playerName引用的EditText EditTextName在activity_diary.xml

你不能這樣做,因爲你已經做到了這一點。

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

因此,爲了view.findViewById任何調用將使用layout_diaryresponse.xml的地方搜索該ID的。

它是空的,因爲@id/EditTextName沒有在那裏定義。


我沒有測試過這一點,但...

「修理」這是投上下文回,它具有@id/EditTextName活性的方法。

假設你創建像這樣

new DiaryAdapter(DiaryActivity.this, cursor, 0); 

您可以在適配器

@Override 
public void bindView(View view, Context context, Cursor cursor) { 

    Activity a = (Activity) context; 
    TextView playerName = (TextView) a.findViewById(R.id.EditTextName); 
+0

感謝@ cricket_007您的初步評論指示我遇到問題,我引用了錯誤的TextView * facepalm *我需要休息一下。再次感謝 – LiamC

+1

很高興我能提供你額外的眼睛;) –

相關問題