Android7.0上Gallery2中长按缩略图后选择edit会闪退的问题修复



android7.0上Gallery2中长按缩略图后选择edit会闪退,报错信息如下:


--------- beginning of crash
11-07 16:40:34.155  3534  3534 E AndroidRuntime: FATAL EXCEPTION: main
11-07 16:40:34.155  3534  3534 E AndroidRuntime: Process: com.android.gallery3d, PID: 3534
11-07 16:40:34.155  3534  3534 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayOptions(int)' on a null object reference
11-07 16:40:34.155  3534  3534 E AndroidRuntime: 	at com.android.gallery3d.filtershow.FilterShowActivity.loadXML(FilterShowActivity.java:352)
11-07 16:40:34.155  3534  3534 E AndroidRuntime: 	at com.android.gallery3d.filtershow.FilterShowActivity.updateUIAfterServiceStarted(FilterShowActivity.java:265)
11-07 16:40:34.155  3534  3534 E AndroidRuntime: 	at com.android.gallery3d.filtershow.pipeline.ProcessingService.onStart(ProcessingService.java:231)
11-07 16:40:34.155  3534  3534 E AndroidRuntime: 	at com.android.gallery3d.filtershow.FilterShowActivity$1.onServiceConnected(FilterShowActivity.java:219)
11-07 16:40:34.155  3534  3534 E AndroidRuntime: 	at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1465)
11-07 16:40:34.155  3534  3534 E AndroidRuntime: 	at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1482)
11-07 16:40:34.155  3534  3534 E AndroidRuntime: 	at android.os.Handler.handleCallback(Handler.java:751)
11-07 16:40:34.155  3534  3534 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:95)
11-07 16:40:34.155  3534  3534 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:154)
11-07 16:40:34.155  3534  3534 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:6077)
11-07 16:40:34.155  3534  3534 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
11-07 16:40:34.155  3534  3534 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
11-07 16:40:34.155  3534  3534 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
11-07 16:40:34.158   427  1702 W ActivityManager:   Force finishing activity com.android.gallery3d/.filtershow.FilterShowActivity

说是FilterShowActivity.java中:
 348     private void loadXML() {
 349         setContentView(R.layout.filtershow_activity);
 350
 351         ActionBar actionBar = getActionBar();
 352         actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
 353         actionBar.setCustomView(R.layout.filtershow_actionbar);
 354         actionBar.setBackgroundDrawable(new ColorDrawable(
 355                 getResources().getColor(R.color.background_screen)));
这里的351行getActionBar()返回的是null,所以导致后面的针对actionBar的方法调用都出错了。

网上很多人说是要修改FilterShowActivity继承自V7中的ActionBarActivity,然后利用getSupportActionBar()方法,试了不行,即使行,这样改也是不可取的,因为改动父类这个改动就很大了,如果是自己写的apk可能还可以考虑,但这个是系统APK,潜在风险太大,果断放弃。后来本着改动最小的宗旨去修复问题,我只修改了一个页面的主题风格,其他的全部保持不变,问题便得到修复。修复方法是修改FilterShow的theme为Theme.Holo.Light.DarkActionBar" 即可:

将res/values/styles.xml中的