gpt4 book ai didi

java - 即使未使用任何服务,也会出现泄漏窗口异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:46 24 4
gpt4 key购买 nike

我遇到了一个非常奇怪的问题。

我的 logcat 中一直出现此崩溃,但奇怪的是我的应用程序与 Text To Speech 无关。不仅仅是这个 Activity ,我的整个应用程序根本不使用它。

08-04 03:47:19.321: E/ActivityThread(24755): Activity com.lablabla.similarsearch.WebViewActivity has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@443065c0 that was originally bound here
08-04 03:47:19.321: E/ActivityThread(24755): android.app.ServiceConnectionLeaked: Activity com.lablabla.similarsearch.WebViewActivity has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@443065c0 that was originally bound here
08-04 03:47:19.321: E/ActivityThread(24755): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:974)
08-04 03:47:19.321: E/ActivityThread(24755): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:868)
08-04 03:47:19.321: E/ActivityThread(24755): at android.app.ContextImpl.bindServiceAsUser(ContextImpl.java:1452)
08-04 03:47:19.321: E/ActivityThread(24755): at android.app.ContextImpl.bindService(ContextImpl.java:1440)
08-04 03:47:19.321: E/ActivityThread(24755): at android.content.ContextWrapper.bindService(ContextWrapper.java:496)
08-04 03:47:19.321: E/ActivityThread(24755): at android.speech.tts.TextToSpeech.connectToEngine(TextToSpeech.java:685)
08-04 03:47:19.321: E/ActivityThread(24755): at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:655)
08-04 03:47:19.321: E/ActivityThread(24755): at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:608)
08-04 03:47:19.321: E/ActivityThread(24755): at android.webkit.AccessibilityInjector$TextToSpeechWrapper.<init>(AccessibilityInjector.java:682)
08-04 03:47:19.321: E/ActivityThread(24755): at android.webkit.AccessibilityInjector.addTtsApis(AccessibilityInjector.java:483)
08-04 03:47:19.321: E/ActivityThread(24755): at android.webkit.AccessibilityInjector.addAccessibilityApisIfNecessary(AccessibilityInjector.java:168)
08-04 03:47:19.321: E/ActivityThread(24755): at android.webkit.AccessibilityInjector.updateJavaScriptEnabled(AccessibilityInjector.java:418)
08-04 03:47:19.321: E/ActivityThread(24755): at android.webkit.WebViewClassic.updateJavaScriptEnabled(WebViewClassic.java:1682)
08-04 03:47:19.321: E/ActivityThread(24755): at android.webkit.WebSettingsClassic.setJavaScriptEnabled(WebSettingsClassic.java:1125)
08-04 03:47:19.321: E/ActivityThread(24755): at com.lablabla.similarsearch.WebViewActivity.onCreate(WebViewActivity.java:24)
08-04 03:47:19.321: E/ActivityThread(24755): at android.app.Activity.performCreate(Activity.java:5133)
08-04 03:47:19.321: E/ActivityThread(24755): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-04 03:47:19.321: E/ActivityThread(24755): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-04 03:47:19.321: E/ActivityThread(24755): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-04 03:47:19.321: E/ActivityThread(24755): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-04 03:47:19.321: E/ActivityThread(24755): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-04 03:47:19.321: E/ActivityThread(24755): at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 03:47:19.321: E/ActivityThread(24755): at android.os.Looper.loop(Looper.java:137)
08-04 03:47:19.321: E/ActivityThread(24755): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-04 03:47:19.321: E/ActivityThread(24755): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 03:47:19.321: E/ActivityThread(24755): at java.lang.reflect.Method.invoke(Method.java:525)
08-04 03:47:19.321: E/ActivityThread(24755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-04 03:47:19.321: E/ActivityThread(24755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-04 03:47:19.321: E/ActivityThread(24755): at dalvik.system.NativeStart.main(Native Method)

下面是该 Activity 的代码:(它所做的只是从 Intent 获取 url 并将其加载到 WebView)

public class WebViewActivity extends Activity {

public static final String EXTRA_URL = "extra_url";

private WebView webView;
private String baseUrl;

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setUserAgentString(
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/20 Safari/537.31");
baseUrl = getIntent().getStringExtra(EXTRA_URL);
webView.loadUrl(baseUrl);
}
}

最佳答案

I keep getting this crash in my logcat, but the weird this is that my app has nothing to do with the Text To Speech thing. not just this activity, my entire app doesn't use it at all.

您的应用确实与它无关。

除了你打电话的时候:

webView.getSettings().setJavaScriptEnabled(true);

WebViewClassic.updateJavaScriptEnabled(boolean) 方法被执行:

void updateJavaScriptEnabled(boolean enabled) {
if (isAccessibilityInjectionEnabled()) {

==>>>> // Call placed to AccessibilityInjector.updateJavaScriptEnabled(boolean)
getAccessibilityInjector().updateJavaScriptEnabled(enabled);
}
}

此处,isAccessibilityInjectionEnabled() 在您设备的情况下返回 true。它检索当前启用的辅助功能服务列表,并提供/支持 AccessibilityServiceInfo.FEEDBACK_SPOKEN。如果此列表非空,则返回 true

向上移动堆栈跟踪:

android.webkit.AccessibilityInjector.addTtsApis()

最终创建一个 TextToSpeech 对象。

问题:TextToSpeech.connectToEngine() 使用 Context 调用 bindService()。上下文是您的 Activity 的上下文(从 WebView 传递)。我相信当 WebViewActivity 启动时,Logcat 会显示这个(或类似的东西):

Sucessfully bound to com.google.android.tts

假设:当您的 Activity 在屏幕旋转时被销毁并重新创建时,TTS 服务仍然绑定(bind),而上下文不再有效。这就是异常所指的。

我尝试了以下方法来检验我的假设:

我没有在 xml 中定义 WebView,而是在 onCreate() 中动态创建它:

webView = new WebView(getApplicationContext());

请注意,我使用应用程序的上下文代替了 Activity 的 (this)。上下文在方向更改时保持有效,因此不会抛出异常。

要确认一项(或多项)无障碍服务已启用,请将此代码添加到您的 Activity 的 onCreate() 中:

AccessibilityManager am = (AccessibilityManager)
getSystemService(ACCESSIBILITY_SERVICE);

List<AccessibilityServiceInfo> listOfServices =
am.getEnabledAccessibilityServiceList(
AccessibilityServiceInfo.FEEDBACK_ALL_MASK);

for (AccessibilityServiceInfo asi : listOfServices) {
Log.i("", "Accessibility Service >>>>>>>>: " + asi.getSettingsActivityName());
Log.i("", "Accessibility Service >>>>>>>>: " +
asi.loadDescription(getPackageManager()));

}

注意:我必须打开 TalkBack(在“设置”>“辅助功能”下)才能重现该问题。另外,我不确定为什么在强制停止应用程序后没有抛出此异常。

根据用户 Robert Nekic 的评论:后退时的行为:使用 Activity 的上下文时会抛出异常。应用程序的上下文仍然有效。

有用信息:Link .

关于java - 即使未使用任何服务,也会出现泄漏窗口异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18038772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com