bug 描述:
(MTK)--Android 11的SystemUI下拉菜单长按图标(tiles)导致SystemUI崩溃重启。
10-01 08:01:11.236 5792 5833 E AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
10-01 08:01:11.236 5792 5833 E AndroidRuntime: Process: com.android.systemui, PID: 5792
10-01 08:01:11.236 5792 5833 E AndroidRuntime: java.lang.RuntimeException: Can't create handler inside thread Thread[AsyncTask #1,5,main] that has not called Looper.prepare()
10-01 08:01:11.236 5792 5833 E AndroidRuntime: at android.os.Handler.<init>(Handler.java:227)
10-01 08:01:11.236 5792 5833 E AndroidRuntime: at android.os.Handler.<init>(Handler.java:129)
10-01 08:01:11.236 5792 5833 E AndroidRuntime: at com.android.systemui.assist.AssistManager.<init>(AssistManager.java:213)
10-01 08:01:11.236 5792 5833 E AndroidRuntime: at com.android.systemui.assist.AssistManager_Factory.provideInstance(AssistManager_Factory.java:107)
10-01 08:01:11.236 5792 5833 E AndroidRuntime: at com.android.systemui.assist.AssistManager_Factory.get(AssistManager_Factory.java:70)
10-01 08:01:11.236 5792 5833 E AndroidRuntime: at com.android.systemui.assist.AssistManager_Factory.get(AssistManager_Factory.java:17)
10-01 08:01:11.236 5792 5833 E AndroidRuntime: at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
10-01 08:01:11.236 5792 5833 E AndroidRuntime: at com.android.systemui.statusbar.phone.StatusBar.lambda$startActivityDismissingKeyguard$16(StatusBar.java:2709)
10-01 08:01:11.236 5792 5833 E AndroidRuntime: at com.android.systemui.statusbar.phone.StatusBar.lambda$startActivityDismissingKeyguard$16$StatusBar(Unknown Source:0)
10-01 08:01:11.236 5792 5833 E AndroidRuntime: at com.android.systemui.statusbar.phone.-$$Lambda$StatusBar$fPMIOsYMhFXVKHESAjUObpcgeJM.run(Unknown Source:10)
10-01 08:01:11.236 5792 5833 E AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
10-01 08:01:11.236 5792 5833 E AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
10-01 08:01:11.236 5792 5833 E AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
10-01 08:01:11.236 5792 5833 E AndroidRuntime: at java.lang.Thread.run(Thread.java:923)
查看报错日志,有点像以前遇到的问题,关于new Handler(),但是没有在主线程里获取Looper ,把目标锁定在了(AssistManager.java:213),查看源码
mAssistDisclosure = new AssistDisclosure(context, new Handler());-->213行
在AssistManager.java里面发现了上述的东西,修改成-->mAssistDisclosure = new AssistDisclosure(context, new Handler(Looper.getMainLooper()));
最后还是报一样的错!!!
和之前碰到的问题不一样,重新查看异常原因:无法在没有调用Looper.prepare()的线程里处理handler。
去查看Handler.java的源码:
public Handler(Callback callback, boolean async) {
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
}
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread " + Thread.currentThread()
+ " that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = callback;
mAsynchronous = async;
}
在实例化 Handler 的时候 Handler 会去检查当前线程的 Looper 是否存在,如果不存在则会报异常,也就是说在创建 Handler 之前一定需要先创建 Looper。
而Looper.java 通过 Looper.prepare 方法来创建 Looper ,并且会借助 ThreadLocal 来实现与当前线程的绑定功能。
Looper.loop 则会开始不断尝试从 MessageQueue 中获取 Message , 并分发给对应的 Handler。
百度救我,关键字搜到一个-->相同问题
第二次修改,在new Handler 之前 创建looper
Looper.prepare();//add
mAssistDisclosure = new AssistDisclosure(context, new Handler());
Looper.loop();//add
编译完成,push之后长按点击下拉菜单中的wifi,SystemUI没有ANR,但是长按也没有跳转进入Setting中wifi设置。
at com.android.systemui.statusbar.phone.StatusBar.lambda$startActivityDismissingKeyguard$16(StatusBar.java:2709)
跟踪代码,发现是这一行用于跳转到对应的activity
+
+ private Runnable lazyRunnable = new Runnable() {
+ @Override
+ public void run() {
+ mAssistManagerLazy.get().hideAssist();
+ }
+ };
+
+
public void startActivityDismissingKeyguard(final Intent intent, boolean onlyProvisioned,
final boolean dismissShade, final boolean disallowEnterPictureInPictureWhileLaunching,
final Callback callback, int flags) {
@@ -2706,7 +2715,8 @@ public class StatusBar extends SystemUI implements DemoMode,
final boolean afterKeyguardGone = mActivityIntentHelper.wouldLaunchResolverActivity(
intent, mLockscreenUserManager.getCurrentUserId());
Runnable runnable = () -> {
- mAssistManagerLazy.get().hideAssist();
+ //mAssistManagerLazy.get().hideAssist();
+ mHandler.post(lazyRunnable);//add
intent.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(flags);
标签:11,10,01,java,08,WiFi,AndroidRuntime,5792,下拉菜单
From: https://www.cnblogs.com/kato-T/p/17808102.html