简体   繁体   English

setText方法上的NullPointerException

[英]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. 下面是我的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)

My DiaryAdapter class is as follows 我的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);

    }

}

So there error arises here: 所以这里出现错误:

playerName.setText(memberName);

playerName references the EditText EditTextName in activity_diary.xml playerName在activity_diary.xml中引用EditText EditTextName

<?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 playerName在activity_diary.xml中引用EditText EditTextName

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. 因此,任何对view.findViewById调用view.findViewById将使用layout_diaryresponse.xml来搜索ID。

It is null because @id/EditTextName is not defined there. 它为null,因为未在其中定义@id/EditTextName


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 . 解决此问题的一种方法是将Context强制转换回确实具有@id/EditTextName的Activity。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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