当我尝试运行此适配器代码时,得到 java.lang.NullPointerException 。我用调试器检查错误。另外,我检查了变量currentbuildingnumber
和BuildingNumber
并打印了log语句
08-07 18:44:25.528 12998-12998/com.example.mustafa.progressupdate V/E1: Adapter Building Number is E1
Activity Building Number is E1
所以我认为问题出在if语句中……但是我不知道为什么它不起作用并且两个变量都被定义并且该语句为真!
适配器代码
public class ActivityAdapter extends ArrayAdapter<Activity> {
/**
* Resource ID for the background color for this list of words
*/
public Activity currentActivity;
public String BuildingNumber;
public String currentbuildingnumber;
public ActivityAdapter(Context context, ArrayList<Activity> Activities, String adapterbuildingnumber) {
super(context, 0, Activities);
// This is the Building number of the Clicked Building
currentbuildingnumber = adapterbuildingnumber;
// This is the Building number of the Activity
BuildingNumber = currentActivity.getBuildingNumber();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.v("E1", "Adapter Building Number is " + currentbuildingnumber);
Log.v("E1", "Activity Building Number is " + BuildingNumber);
View v = convertView;
if(BuildingNumber == currentbuildingnumber) {
// Check if an existing view is being reused, otherwise inflate the view
if (v == null) {
v = LayoutInflater.from(getContext()).inflate(R.layout.project_actvitiy_list, parent, false);
}
// Get the {@link Word} object located at this position in the list
currentActivity = getItem(position);
TextView Act_ID = (TextView) v.findViewById(R.id.Act_ID);
TextView Act_Name = (TextView) v.findViewById(R.id.Act_Name);
TextView Act_Start = (TextView) v.findViewById(R.id.Act_Start);
TextView Act_Prog = (TextView) v.findViewById(R.id.Act_Prog);
TextView Act_Finish = (TextView) v.findViewById(R.id.Act_Finish);
TextView Act_Note = (TextView) v.findViewById(R.id.Act_Note);
Act_ID.setText(String.valueOf(currentActivity.getActivityID()));
Act_Name.setText(String.valueOf(currentActivity.getActivityName()));
Act_Start.setText(String.valueOf(currentActivity.getActualStart()));
Act_Prog.setText(String.valueOf(currentActivity.getActivityProgress()));
Act_Finish.setText(String.valueOf(currentActivity.getActualFinish()));
Act_Note.setText(String.valueOf(currentActivity.getNotes()));
Log.v("E1", "Activity ID is " + currentActivity.getActivityID());
Log.v("E1", "Activity Name is " + currentActivity.getActivityName());
Log.v("E1", "Activity Actual Start is " + currentActivity.getActualStart());
Log.v("E1", "Activity Progress is " + currentActivity.getActivityProgress());
Log.v("E1", "Activity Actual Finish is " + currentActivity.getActualFinish());
Log.v("E1", "Activity Notes is " + currentActivity.getNotes());
}
return v;
}
}
崩溃日志
--------- beginning of crash
08-07 19:59:21.764 14227-14227/com.example.mustafa.progressupdate E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mustafa.progressupdate, PID: 14227
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference
at android.widget.AbsListView.obtainView(AbsListView.java:2379)
at android.widget.ListView.makeAndAddView(ListView.java:1970)
at android.widget.ListView.fillDown(ListView.java:704)
at android.widget.ListView.fillFromTop(ListView.java:765)
at android.widget.ListView.layoutChildren(ListView.java:1744)
at android.widget.AbsListView.onLayout(AbsListView.java:2161)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
08-07 19:59:21.765 1660-2992/system_process W/ActivityManager: Force finishing activity com.example.mustafa.progressupdate/.E1
08-07 19:59:21.767 1785-2008/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xa0c38200: ver 3 1 (tinfo 0xa0c7f460)
08-07 19:59:21.772 1660-2992/system_process W/ActivityManager: Force finishing activity com.example.mustafa.progressupdate/.SoleyaProjectSchedule
08-07 19:59:21.790 1660-1683/system_process I/WindowManager: Screen frozen for +190ms due to Window{36e1b87 u0 NavigationBar}
08-07 19:59:21.819 1785-2008/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xa0c38200: ver 3 1 (tinfo 0xa0c7f460)
08-07 19:59:21.832 1785-2008/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xa0c38200: ver 3 1 (tinfo 0xa0c7f460)
08-07 19:59:21.844 1660-1729/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
08-07 19:59:21.844 1660-1729/system_process D/OpenGLRenderer: Swap behavior 1
08-07 19:59:21.845 1660-1729/system_process W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
08-07 19:59:21.845 1660-1729/system_process D/OpenGLRenderer: Swap behavior 0
08-07 19:59:21.848 1660-1729/system_process D/EGL_emulation: eglCreateContext: 0x92fa0300: maj 3 min 1 rcv 4
08-07 19:59:21.856 1660-1729/system_process D/EGL_emulation: eglMakeCurrent: 0x92fa0300: ver 3 1 (tinfo 0x9257e590)
08-07 19:59:21.859 1660-1729/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
glUtilsParamSize: unknow param 0x00008cdf
08-07 19:59:21.860 1660-1729/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
08-07 19:59:21.867 1660-1729/system_process D/EGL_emulation: eglMakeCurrent: 0x92fa0300: ver 3 1 (tinfo 0x9257e590)
08-07 19:59:22.274 1660-1676/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{96f635b u0 com.example.mustafa.progressupdate/.E1 t153 f}
08-07 19:59:24.800 1399-1422/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 14558875 , only wrote 14290560
08-07 19:59:27.831 1660-1683/system_process I/art: Starting a blocking GC Explicit
08-07 19:59:27.857 1660-1683/system_process I/art: Explicit concurrent mark sweep GC freed 20596(1621KB) AllocSpace objects, 6(120KB) LOS objects, 26% free, 11MB/15MB, paused 846us total 26.282ms
08-07 19:59:30.905 1660-1739/system_process I/WifiConnectivityManager: start a single scan from watchdogHandler
scheduleWatchdogTimer
08-07 19:59:31.600 1660-1676/system_process W/ActivityManager: Launch timeout has expired, giving up wake lock!
08-07 19:59:31.777 1660-1676/system_process W/ActivityManager: Activity destroy timeout for ActivityRecord{52aa486 u0 com.example.mustafa.progressupdate/.SoleyaProjectSchedule t153 f}
08-07 20:00:01.040 1785-2008/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xa0c38200: ver 3 1 (tinfo 0xa0c7f460)
'
答案 0 :(得分:1)
问题在于您有一个巨大的if
块,有时会避免使用大多数getView()
方法:
@Override public View getView(int position, View convertView, ViewGroup parent) { Log.v("E1", "Adapter Building Number is " + currentbuildingnumber); Log.v("E1", "Activity Building Number is " + BuildingNumber); View v = convertView; if(BuildingNumber == currentbuildingnumber) { ... } return v; }
这意味着有时候您只是返回convertView
的值,而那个有时可以是null
。这是个问题; getView()
绝不应该返回null!
您必须更改代码以处理这种情况。也许您可以将此代码块移至主要if
之外:
// Check if an existing view is being reused, otherwise inflate the view
if (v == null) {
v = LayoutInflater.from(getContext()).inflate(R.layout.project_actvitiy_list, parent, false);
}
那样,您将知道v
在返回时永远不会为空。
答案 1 :(得分:0)