简体   繁体   中英

NullPointerException on setText method

I have been debugging this for over an hour now but cannot find the solution. I have a created a sports team application for a university project and I am having members of the team complete a daily diary. The manager of the team can then view the individual diary responses.

Where I am having the issue is returning the information from the database. Below is my logcat which indicates the problem is at line 58 of my DiaryAdapter class.

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)

My DiaryAdapter class is as follows

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);

    }

}

So there error arises here:

playerName.setText(memberName);

playerName references the EditText EditTextName in 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" />

I would greatly appreciate another set of eyes on this issue. Thanks in advance.

playerName references the EditText EditTextName in activity_diary.xml

You can't do that because you have done this.

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

Therefore any call to view.findViewById will use layout_diaryresponse.xml for where it searches for the ID's.

It is null because @id/EditTextName is not defined there.


I haven't tested this, but...

A way to "fix" this is to cast the Context back to the Activity that does have @id/EditTextName .

Assuming you created the Adapter like so

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

You can do this in the adapter

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM