我现在已经调试了一个多小时但是找不到解决方案。我为一个大学项目创建了一个运动团队应用程序,我正在让团队成员完成每日日记。然后,团队经理可以查看单独的日记回复。
我遇到的问题是从数据库返回信息。下面是我的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" />
我非常感谢关于这个问题的另一双眼睛。提前谢谢。
答案 0 :(得分:2)
playerName引用activity_diary.xml中的EditText EditTextName
你不能这样做,因为你已经这样做了。
@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的位置。
它为null,因为那里没有定义@id/EditTextName
。
我没有测试过这个,但是......
“修复”这种方法是将Context强制转换为具有@id/EditTextName
的Activity。
假设你像这样创建了适配器
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);