Java setText方法上的NullPointerException

Java setText方法上的NullPointerException,java,android,Java,Android,我已经调试了一个多小时了,但是找不到解决方案。我已经为一个大学项目创建了一个体育团队申请,我正在让团队成员完成一份每日日记。然后,团队经理可以查看个人日记回复 我遇到的问题是从数据库返回信息。下面是我的logcat,它表明问题出现在我的DiaryAdapter类的第58行 01-04 16:27:05.339 23442-23442/com.example.myacer.clubhub E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.

我已经调试了一个多小时了,但是找不到解决方案。我已经为一个大学项目创建了一个体育团队申请,我正在让团队成员完成一份每日日记。然后,团队经理可以查看个人日记回复

我遇到的问题是从数据库返回信息。下面是我的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引用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" />

我将非常感谢在这个问题上再多看一眼。提前谢谢

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" />
你不能那样做,因为你已经这样做了

 @Override
 public View newView(Context context, Cursor cursor, ViewGroup parent) {
     return LayoutInflater.from(context).inflate(R.layout.layout_diaryresponse, parent, false);
 }
因此,对
view.findViewById
的任何调用都将使用
layout\u 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);

您真的使用activity_diary.xml布局作为视图吗?您是否检查过哪一个为空、
playerName
memberName
?我假设它是其中之一。看起来您使用的是
R.layout.layout\u diaryresponse
,而不是
activity\u diary
。你不能从适配器中引用它。谢谢@cricket_007你最初的评论将我引向了这个问题,我引用了错误的TextView facepalm,我需要休息一下。谢谢你,我可以为你提供额外的一套眼睛;)