gpt4 book ai didi

java - WebViewClient.shouldOverrideUrlLoading 中的 FragmentTransaction 抛出 IllegalStateException

转载 作者:太空狗 更新时间:2023-10-29 16:02:27 24 4
gpt4 key购买 nike

我似乎在与竞争条件作斗争,我似乎无法确定其原因。执行下面的代码时,我间歇性地得到下面的堆栈跟踪。

我是否违反了 Fragment 生命周期的一些明显规则?我不清楚什么会明确禁止我在这里执行交易来处理事件。

我正在使用 WebViewClient 来检测在本地 .html 文档中单击的外部 URL——例如,指向非本地主机的 URL。我正在使用 Otto 的 EventBus 将这些操作发布到 Activity。当 Activity 收到这些事件时,我想通过调用 FragmentTransaction.replace()

在不同的 Fragment 中显示这些外部 URL >

DefaultWebViewClient.java

@Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
boolean shouldOverride;
if (urlIsLocal(url)) {
shouldOverride = super.shouldOverrideUrlLoading(view, url);
} else {
// trigger an event for the fragment to swap out
// return true to tell the webview not to load it...
EventBus.getInstance().post(new LoadExternalUrlEvent(url));
shouldOverride = true;
}
return shouldOverride;
}

FragmentActivity.java

@Subscribe
public void onLoadExternalUrlEvent(LoadExternalUrlEvent externalLoadEvent) {
final BrowserFragment browserFragment = new BrowserFragment();
Bundle args = new Bundle();
args.putSerializable(BrowserFragment.ARG_LOAD_EXTERNAL_URL_EVENT, externalLoadEvent);
browserFragment.setArguments(args);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, browserFragment, BrowserFragment.FRAGMENT_TAG)
.addToBackStack(null).commit();
}

LoadExternalUrlEvent.java

public class LoadExternalUrlEvent implements Serializable {

private static final long serialVersionUID = 1L;

public final String url;

public LoadExternalUrlEvent(String url) {
this.url = url;
}

@Override
public String toString() {
return "LoadExternalUrlEvent [url=" + url + "]";
}

}

EventBus.java

import com.squareup.otto.Bus;

public class EventBus {

private static Bus _INSTANCE;

public static synchronized Bus getInstance() {
if (null == _INSTANCE) {
_INSTANCE = new Bus();
}
return _INSTANCE;
}

}

堆栈跟踪

 java.lang.RuntimeException: Could not dispatch event: class <omitted>.LoadExternalUrlEvent to handler [EventHandler public void <omitted>Activity.onLoadExternalUrlEvent(<omitted>LoadExternalUrlEvent)]: Can not perform this action after onSaveInstanceState
at com.squareup.otto.Bus.throwRuntimeException(Bus.java:456)
at com.squareup.otto.Bus.dispatch(Bus.java:386)
at com.squareup.otto.Bus.dispatchQueuedEvents(Bus.java:367)
at com.squareup.otto.Bus.post(Bus.java:336)
at <omitted>DefaultWebViewClient.shouldOverrideUrlLoading(DefaultWebViewClient.java:51)
at com.android.webview.chromium.WebViewContentsClientAdapter.shouldOverrideUrlLoading(WebViewContentsClientAdapter.java:293)
at com.android.org.chromium.android_webview.AwContentsClientBridge.shouldOverrideUrlLoading(AwContentsClientBridge.java:96)
at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1360)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1378)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
at <omitted>Activity.run(<omitted>Activity.java:162)
at <omitted>Activity.onLoadExternalUrlEvent(<omitted>Activity.java:156)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.squareup.otto.EventHandler.handleEvent(EventHandler.java:89)
at com.squareup.otto.Bus.dispatch(Bus.java:384)
... 15 more
A/libc(2689): Fatal signal 6 (SIGABRT) at 0x00000a81 (code=-6), thread 2689

最佳答案

我发现了问题。

因为我在 Activity.onCreate() 中调用了 EventBus.register() 我在后台获得了 Activity 的多个实例它将作为这些事件的响应者。

解决方案是尽可能晚地注册您的Activity

@Override
protected void onResume() {
super.onResume();
EventBus.getInstance().register(this);
}

@Override
protected void onPause() {
EventBus.getInstance().unregister(this);
super.onPause();
}

或者将您的 Activity 声明为单个实例

android:launchMode="singleTask"

关于java - WebViewClient.shouldOverrideUrlLoading 中的 FragmentTransaction 抛出 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27466644/

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