奇怪的空指针异常

03-07 18:25:12.890  6447  6447 E AndroidRuntime: FATAL EXCEPTION: main
03-07 18:25:12.890  6447  6447 E AndroidRuntime: Process: com.android.settings, PID: 6447
03-07 18:25:12.890  6447  6447 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:445)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.widget.ArrayAdapter.getView(ArrayAdapter.java:407)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.widget.AbsListView.obtainView(AbsListView.java:2365)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.widget.ListView.measureHeightOfChildren(ListView.java:1408)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.widget.ListView.onMeasure(ListView.java:1315)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.View.measure(View.java:22248)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.View.measure(View.java:22248)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at com.android.internal.widget.AlertDialogLayout.tryOnMeasure(AlertDialogLayout.java:144)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at com.android.internal.widget.AlertDialogLayout.onMeasure(AlertDialogLayout.java:69)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.View.measure(View.java:22248)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.View.measure(View.java:22248)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.View.measure(View.java:22248)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6682)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at com.android.internal.policy.DecorView.onMeasure(DecorView.java:725)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.View.measure(View.java:22248)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2523)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1550)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1818)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1438)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6982)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:913)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.Choreographer.doCallbacks(Choreographer.java:725)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.Choreographer.doFrame(Choreographer.java:660)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:899)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.os.Handler.handleCallback(Handler.java:790)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:164)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:6548)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
03-07 18:25:12.896   589  2047 W ActivityManager:   Force finishing activity com.android.settings/.Settings$ApnEditorActivity

看到上面的报错信息,是不是有点懵逼,因为它并没有给出某个具体类的报错行数,而且一眼看下去都是framework层的类,对于framework层,我们并没有去修改什么,而且在没有找到具体的报错原因的时候,不能在framework层去修改,因为会引起其他隐形的问题。
既然如此我们又改如何分析呢?
在报错信息中我们看到了应用层有个类停止运行:

03-07 18:25:12.896   589  2047 W ActivityManager:   Force finishing activity com.android.settings/.Settings$ApnEditorActivity

ApnEditorActivity只能从这个类中入手了,再分析一下这个必现问题的复现路径,在设置中,插卡情况下, 点击APN下的MVNO type,Settings has stopping.从下面的log中可看到,它是在加载资源的时候出现为空,所以去查看它调用的资源。

03-07 18:25:12.890  6447  6447 E AndroidRuntime: 	at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:445)

通过查看资源文件,发现第一个item是为空的:

 <string-array name="ext_mvno_type_entries" translatable="false">
        <item>@string/mvno_type_none</item>
        <item>SPN</item>
        <item>IMSI</item>
        <item>GID</item>
        <item>PNN</item>
    </string-array>

    <string-array name="ext_mvno_type_values" translatable="false">
        <item>""</item>
        <item>spn</item>
        <item>imsi</item>
        <item>gid</item>
        <item>pnn</item>
    </string-array>

去搜索@string/mvno_type_none字符串,发现在Values中并没有去定义它,但是在其他语言下是有定义的

./res_ext/values-en-rGB/mtk_strings.xml:   <string name="mvno_type_none">"None"</string>
./res_ext/values-ca/mtk_strings.xml:   <string name="mvno_type_none">"Cap"</string>
./res_ext/values-hy-rAM/mtk_strings.xml:   <string name="mvno_type_none">"Ոչ մեկը"</string>
./res_ext/values-mn/mtk_strings.xml:   <string name="mvno_type_none">কোনটি নয়</string>
./res_ext/values-brx/mtk_strings.xml:   <string name="mvno_type_none">कोई नहीं</string>
./res_ext/values-te/mtk_strings.xml:   <string name="mvno_type_none">कोई नहीं</string>
.......

到此为止,问题是定位到了,我在values下去定义了这个字符串,发现问题完美解决。

./res_ext/values/mtk_strings.xml:   <string name="mvno_type_none">"None"</string>

总结:一般空指针异常,如果log中没有精确定位到某个类的报错行数,那应该就去资源文件中看看。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值