Android通过崩溃日志中控件id定位崩溃位置的方法

Posted Vigibord

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android通过崩溃日志中控件id定位崩溃位置的方法相关的知识,希望对你有一定的参考价值。

问题

有时候我们获取到的崩溃日志中没有明确的哪个自定义类发生崩溃,有效信息只有系统的类或者某个控件类,这样我们就无法定位到具体的崩溃位置,比如下面这个崩溃:

java.lang.IllegalStateException: The content of the adapter has changed but
ListView did not receive a notification. Make sure the content of your adapter
is not modified from a background thread, but only from the UI thread. [in
ListView(2131559894, class android.widget.ListView) with Adapter(class
android.widget.HeaderViewListAdapter)]
         at android.widget.ListView.layoutChildren(ListView.java:1551)
         at android.widget.AbsListView.onTouchEvent(AbsListView.java:3605)
         at android.view.View.dispatchTouchEvent(View.java:7315)
         at
android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2253)
         at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1950)
         at
android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259)
         at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
         at
android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259)
         at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
         at
android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259)
         at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
         at
android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259)
         at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
         at
android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259)
         at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
         at
android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259)
         at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1964)
         at
com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1970)
         at
com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1420)
         at android.app.Activity.dispatchTouchEvent(Activity.java:2444)
         at
com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1918)
         at android.view.View.dispatchPointerEvent(View.java:7506)
         at
android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3680)
         at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3605)
         at
android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4875)
         at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4835)
         at
android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4991)
         at
android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
         at android.os.MessageQueue.nativePollOnce(Native Method)
         at android.os.MessageQueue.next(MessageQueue.java:128)
         at android.os.Looper.loop(Looper.java:151)
         at android.app.ActivityThread.main(ActivityThread.java:5398)
         at java.lang.reflect.Method.invokeNative(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:525)
         at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
         at dalvik.system.NativeStart.main(Native Method)

上面这个崩溃就不能明显的显示出崩溃位置,但是还是透露了蛛丝马迹给我们查找,看这里[in ListView(2131559894, class android.widget.ListView) with Adapter(class android.widget.HeaderViewListAdapter)],标红的一串数字就是该listview的id,从源码中可以看到:

                throw new IllegalStateException("The content of the adapter has changed but "
                        + "ListView did not receive a notification. Make sure the content of "
                        + "your adapter is not modified from a background thread, but only from "
                        + "the UI thread. Make sure your adapter calls notifyDataSetChanged() "
                        + "when its content changes. [in ListView(" + getId() + ", " + getClass()
                        + ") with Adapter(" + mAdapter.getClass() + ")]");

解决办法

得到控件的数字id后如何反推出控件的具体使用位置呢?因为每次编译之后R文件都是临时生成的,所有的id都是会变的,所以需要两个原材料:
A. 找到该崩溃对应的app版本,拿到apk包;
B. 一个合适的反编译工具,这里推荐jadx,绿色快速。连接http://download.csdn.net/detail/zhaoshuyu111/9749253

反编译后在resources.arsc中搜索数字id,通过其对应的有意义的字符串id,就能定位到崩溃位置了;
如图:

以上是关于Android通过崩溃日志中控件id定位崩溃位置的方法的主要内容,如果未能解决你的问题,请参考以下文章

常用获取Android崩溃日志和IOS崩溃日志的几种方法

一次Android App NDK崩溃问题的分析及解决

核心定位崩溃

Swift 伪原子并发同步代码引起 DispatchGroup.leave() 方法不平衡调用导致 App 崩溃的解决

Swift 伪原子并发同步代码引起 DispatchGroup.leave() 方法不平衡调用导致 App 崩溃的解决

Android App在用户手上一启动就退出了(崩溃),有啥工具能拿到用户日志吗?