泛泰A800S移植A850天气软件debug流程

来源:互联网 发布:网络机顶盒安装视频 编辑:程序博客网 时间:2024/06/07 19:20
前几天听 syhost 说850自带的天气软件天气软件还不错,看了一下的确还行,想想既然820上可用,弄到800上来应该也是顺理成章的事情,于是动手干吧。
结果弄到800上安装好,一运行马上报错,意料之中,于是开adb打log,log中有效信息如下:
E/AndroidRuntime(14257): FATAL EXCEPTION: mainE/AndroidRuntime(14257): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pantech.weather/com.pantech.weather.app.WeatherDetail}: java.lang.NullPointerExceptionE/AndroidRuntime(14257):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1961)E/AndroidRuntime(14257):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1986)E/AndroidRuntime(14257):     at android.app.ActivityThread.access$600(ActivityThread.java:128)E/AndroidRuntime(14257):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1152)E/AndroidRuntime(14257):     at android.os.Handler.dispatchMessage(Handler.java:99)E/AndroidRuntime(14257):     at android.os.Looper.loop(Looper.java:137)E/AndroidRuntime(14257):     at android.app.ActivityThread.main(ActivityThread.java:4449)E/AndroidRuntime(14257):     at java.lang.reflect.Method.invokeNative(Native Method)E/AndroidRuntime(14257):     at java.lang.reflect.Method.invoke(Method.java:511)E/AndroidRuntime(14257):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)E/AndroidRuntime(14257):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)E/AndroidRuntime(14257):     at dalvik.system.NativeStart.main(Native Method)E/AndroidRuntime(14257):     Caused by: java.lang.NullPointerExceptionE/AndroidRuntime(14257):     at com.pantech.weather.app.WeatherDetail.onCreate(WeatherDetail.java:124)E/AndroidRuntime(14257):     at android.app.Activity.performCreate(Activity.java:4465)E/AndroidRuntime(14257):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)E/AndroidRuntime(14257):     at com.lbe.security.service.core.client.b.aj.callActivityOnCreate(Unknown Source)E/AndroidRuntime(14257):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1925)E/AndroidRuntime(14257):     ... 11 more
从上面可以看到WeatherDetail中的onCreate()函数是问题的关键。于是反编译Weather.apk并定位到WeatherDetail.onCreate()函数中,发现真正出问题的是下面这句代码:
    .line 121    new-instance v2, Landroid/view/GestureDetector;    new-instance v3, Lcom/pantech/weather/app/WeatherDetail$ScrollViewDetector;    invoke-direct {v3, p0}, Lcom/pantech/weather/app/WeatherDetail$ScrollViewDetector;-><init>(Lcom/pantech/weather/app/WeatherDetail;)V    invoke-direct {v2, v3}, Landroid/view/GestureDetector;-><init>(Landroid/view/GestureDetector$OnGestureListener;)V    iput-object v2, p0, Lcom/pantech/weather/app/WeatherDetail;->mGestrDetector:Landroid/view/GestureDetector;    .line 123    invoke-virtual {p0}, Lcom/pantech/weather/app/WeatherDetail;->getActionBar()Landroid/app/ActionBar;    move-result-object v0            .line 124    .local v0, action:Landroid/app/ActionBar;
上面报错空指针的是124行,实际看很明显是123行处getActionBar()返回值的问题,猜想应该是此函数返回给局部变量v0中的是空指针,导致了124行的报错。既然明确了问题所在,当然就是验证了。这时候当然要用到xuefy大神的smali专用调试log包了,加入xuefy大神的调试函数,然后在上述代码后加上log代码如下:
#   crazyi debug begin        const-string v2, "ActionBar Adress 4433: "            invoke-direct {p0, v2, v0}, Lcom/pantech/weather/app/WeatherDetail;->xuefy_Log_object(Ljava/lang/String;Ljava/lang/Object;)V            const-string v2, "WeatherDetail Adress 4433: "            invoke-direct {p0, v2, p0}, Lcom/pantech/weather/app/WeatherDetail;->xuefy_Log_object(Ljava/lang/String;Ljava/lang/Object;)V#   crazyi debug end

xuefy_Log_object(Ljava/lang/String;Ljava/lang/Object;)函数可以将一个Java对象的内存地址(即指针)打印到log,于是我打印了2个,分别是WeatherDetail这个activity的地址和ActionBar这个从属于WeatherDetail的地址。

打印log以后不出所料,getActionBar()的返回值果然为空,而WeatherDetail却不为空,这说明WeatherDetail这个activity创建成功,而从属于WeatherDetail的ActionBar却没有。

E/xuefy   ( 3898): ActionBar Adress 4433: nullE/xuefy   ( 3898): WeatherDetail Adress 4433: com.pantech.weather.app.WeatherDetail@41898c38
于是我在想是否是getActionBar()的调用顺序问题,网上有人提到过setContentView()函数要在getActionBar()之前运行才行,不然getActionBar()会返回空值,但是检查代码后发现泛泰貌似没有犯这种错误。然后在其他类似调用getActionBar()的地方也添加了上述的调试代码,但是由于onCreate()函数是在activity创建时被调用,优先级明显高于其他调用getActionBar()的地方,所以最后报错的依然还是开始的那个地方,其他地方则是来不及报错程序已经结束了。
在smali代码中找不出原因,于是我想是不是缺了某些资源,于是对比820rom中的相关文件和框架,没有发现什么有价值的地方;然后去翻google 的官方API文档,同样一无所获,折腾半天毫无收获,于是就搁置了。
过了几天,随着800的长短信问题在syhost的帮助下搞定,又决定回过头来弄这个问题,于是在google中搜索了一下NullPointerException+getActionBar,出人意料,貌似碰到这种问题的人还真不少,仔细一看,原来是getActionBar()会牵扯到AndroidManifest.xml中的主题问题,如果在AndroidManifest.xml中设置了theme,在activity中使用getActionBar()函数就会返回空指针异常。而解决方法超级简单,直接去掉AndroidManifest.xml中的theme属性即可。
例如:
        <activity android:theme="@*android:style/Theme.DeviceDefault.SearchBar" android:label="@string/app_name" android:name=".app.WeatherDetail" android:screenOrientation="portrait">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </activity>
改为:
        <activity android:label="@string/app_name" android:name=".app.WeatherDetail" android:screenOrientation="portrait">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </activity>
很简单的一个问题却走了那么多的弯路,回想起来真的很让人哭笑不得。同时想鄙视一下google,这么大的问题竟然在API文档中没有给过一句提示,要不是有人遇到过让我自己找估计一辈子都难搞得明白。

附录一下参考网站:
http://blog.perpetumdesign.com/2011/08/strange-case-of-dr-action-and-mr-bar.html
http://stackoverflow.com/questions/6867076/getactionbar-returns-null
http://stackoverflow.com/questions/5914791/android-3-0-action-bar-dont-want-to-go
http://blog.csdn.net/crazy123456789/article/details/7495590


原创粉丝点击