gpt4 book ai didi

java - DaysAdapter.getView(DaysFragment.java :117) 处的 NPE

转载 作者:行者123 更新时间:2023-12-01 12:28:25 31 4
gpt4 key购买 nike

直觉上我知道为什么我们会得到 NPE 异常,但在这种情况下我找不到原因。也许这只是一天的结束。我在这条线上得到了 NPE

viewHolder.dayCaption.setText(storage.getDayName(i));

我使用此代码来记录:

Log.d("MTEST","viewHolder="+viewHolder+"\nviewHolder.dayCaption="+viewHolder.dayCaption+"\nstorage.getDayName(i)="+storage.getDayName(i));

并得到这个值:

viewHolder=com.bbc.ddm.fragments.DaysFragment$DaysAdapter$ViewHolder@423df950
viewHolder.dayCaption= android.widget.TextView@423ded08

storage.getDayName(i)=Monday

这充满了适配器

 private class DaysAdapter extends BaseAdapter implements AdapterView.OnItemClickListener{

@Override
public int getCount() {
return dayEnabled.length;
}

@Override
public Object getItem(int i) {
return null;
}

@Override
public long getItemId(int i) {
return i;
}
private class ViewHolder {
TextView dayCaption;
CheckBox dayCheck;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;

if (view == null) {
view = getActivity().getLayoutInflater().inflate(R.layout.day_list_item, viewGroup, false);
viewHolder = new ViewHolder();
viewHolder.dayCaption = (TextView) view.findViewById(R.id.day_caption);
viewHolder.dayCheck = (CheckBox) view.findViewById(R.id.day_check);
} else {
viewHolder = (ViewHolder) view.getTag();
}
storage = Storage.getInstance();
// Log.d("MTEST","viewHolder="+viewHolder+"\nviewHolder.dayCaption= "+viewHolder.dayCaption+"\nstorage.getDayName(i)="+storage.getDayName(i));
viewHolder.dayCaption.setText(storage.getDayName(i));
viewHolder.dayCheck.setTag(i);
viewHolder.dayCheck.setChecked(dayEnabled[i]);
viewHolder.dayCheck.setOnCheckedChangeListener(checkedChangeListener);

return view;
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
activity.showDayTime(i);
}




}

实际日志是

10-01 17:18:19.176: E/AndroidRuntime(4455): FATAL EXCEPTION: main
10-01 17:18:19.176: E/AndroidRuntime(4455): java.lang.NullPointerException
10-01 17:18:19.176: E/AndroidRuntime(4455): at com.revolage.eyespause.fragments.DaysFragment$DaysAdapter.getView(DaysFragment.java:120)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.widget.AbsListView.obtainView(AbsListView.java:2255)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.widget.ListView.measureHeightOfChildren(ListView.java:1269)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.widget.ListView.onMeasure(ListView.java:1181)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.View.measure(View.java:15323)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1421)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.widget.LinearLayout.onMeasure(LinearLayout.java:579)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.View.measure(View.java:15323)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.widget.FrameLayout.onMeasure(FrameLayout.java:315)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.View.measure(View.java:15323)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.widget.FrameLayout.onMeasure(FrameLayout.java:315)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.View.measure(View.java:15323)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:622)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:404)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.View.measure(View.java:15323)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.widget.FrameLayout.onMeasure(FrameLayout.java:315)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.View.measure(View.java:15323)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.widget.LinearLayout.onMeasure(LinearLayout.java:579)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.View.measure(View.java:15323)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.widget.FrameLayout.onMeasure(FrameLayout.java:315)
10-01 17:18:19.176: E/AndroidRuntime(4455): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2155)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.View.measure(View.java:15323)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1854)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4218)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.Choreographer.doFrame(Choreographer.java:525)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.os.Handler.handleCallback(Handler.java:615)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.os.Handler.dispatchMessage(Handler.java:92)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.os.Looper.loop(Looper.java:137)
10-01 17:18:19.176: E/AndroidRuntime(4455): at android.app.ActivityThread.main(ActivityThread.java:4744)
10-01 17:18:19.176: E/AndroidRuntime(4455): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 17:18:19.176: E/AndroidRuntime(4455): at java.lang.reflect.Method.invoke(Method.java:511)
10-01 17:18:19.176: E/AndroidRuntime(4455): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-01 17:18:19.176: E/AndroidRuntime(4455): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-01 17:18:19.176: E/AndroidRuntime(4455): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您忘记了 setTag() 将 View 持有者存储在 View 中。在回收 View 上,使用 getTag() 检索的持有者将为 null。

添加

view.setTag(viewHolder);

位于 if (view == null) block 的末尾。

关于java - DaysAdapter.getView(DaysFragment.java :117) 处的 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26138584/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com