首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在android studio中使用自定义适配器时出现"java.lang.NullPointerException“错误

在android studio中使用自定义适配器时出现"java.lang.NullPointerException“错误
EN

Stack Overflow用户
提问于 2018-08-08 01:48:50
回答 1查看 68关注 0票数 -1

当我尝试运行此适配器代码时,我得到java.lang.NullPointerException.我使用调试器检查错误。此外,我还检查了变量currentbuildingnumberBuildingNumber以及打印的日志语句

代码语言:javascript
复制
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语句中...然而,我不知道为什么它不能工作,两个变量都定义了,并且语句是真的!

适配器代码

代码语言:javascript
复制
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;
}
}

故障日志

代码语言:javascript
复制
--------- 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)
'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-08 01:54:29

==测试引用是否相等(无论它们是不是同一个对象)。

.equals()测试值是否相等(它们在逻辑上是否“相等”)。

more info

==更改为.equals

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51732653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档