欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CRASH问题:java.lang.NullPointerException

程序员文章站 2022-04-09 10:30:26
...

java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.ImageView.setImageResource(int)’ on a null object reference

今天在家练习ListView的时候,遇到一个java.lang.NullPointerException的空指针异常,log如下:

2020-05-10 15:40:29.249 20436-20436/? E/k.myapplicatio: Unknown bits set in runtime_flags: 0x8000
2020-05-10 15:40:29.602 20436-20436/com.hpeak.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.hpeak.myapplication, PID: 20436
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
        at com.hpeak.myapplication.MainActivity$MyAdapter.getView(MainActivity.java:72)
        at android.widget.AbsListView.obtainView(AbsListView.java:2437)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1423)
        at android.widget.ListView.onMeasure(ListView.java:1329)
        at android.view.View.measure(View.java:25253)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6891)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
        at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1204)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:723)
        at android.view.View.measure(View.java:25253)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6891)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:25253)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6891)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
        at android.view.View.measure(View.java:25253)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6891)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:851)
        at android.view.View.measure(View.java:25253)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3419)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2124)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2426)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2008)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8600)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1036)
        at android.view.Choreographer.doCallbacks(Choreographer.java:859)
        at android.view.Choreographer.doFrame(Choreographer.java:794)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1021)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:247)
        at android.app.ActivityThread.main(ActivityThread.java:8263)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:511)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)

根据log信息依次定位到下图所示代码处:
CRASH问题:java.lang.NullPointerException
CRASH问题:java.lang.NullPointerException
暂时看代码并没有看出有什么问题,然后又看了一遍代码,加了log,将imgResIds[position]打印出来,发现打印不出来内容,又发现setImageResource(imgResIds[position])是由imageView调用的,仔细分析了一下imageView的来源,终于发现是imageView的问题,故解决方法是:
将代码

//            1.解析布局文件,成为view
            LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
            View view = inflater.inflate(R.layout.list_item, parent, false);
//            2.填充当前项的数据
            ImageView imageView = findViewById(R.id.img1);
            imageView.setImageResource(imgResIds[position]);
            
            TextView textView = view.findViewById(R.id.text1);
            textView.setText(names[position]);

修改为

//            1.解析布局文件,成为view
            LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
            View view = inflater.inflate(R.layout.list_item, parent, false);
//            2.填充当前项的数据
            ImageView imageView = view.findViewById(R.id.img1);
            imageView.setImageResource(imgResIds[position]);

            TextView textView = view.findViewById(R.id.text1);
            textView.setText(names[position]);

再次运行,正常。运行结果如下图所示:

CRASH问题:java.lang.NullPointerException
至此,问题解决。