Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android片段(使用WebView)-活动被销毁_Android_Android Fragments_Android Webview_Webviewclient - Fatal编程技术网

Android片段(使用WebView)-活动被销毁

Android片段(使用WebView)-活动被销毁,android,android-fragments,android-webview,webviewclient,Android,Android Fragments,Android Webview,Webviewclient,我正在我的应用程序中使用一个基于选项卡的活动,我想用WebView实现一个片段堆栈。在这些WebView中,我希望捕获URL请求并用新的WebView片段替换当前的WebView片段。不幸的是,我总是得到活动被破坏的异常,但不知道为什么 你能给我一些提示如何解决这个问题吗 我使用以下代码(在代码示例的末尾,您将识别我要替换当前片段的方法handleUrlRequest): 我终于明白了。我通过WebViewClient传递了Web实例,在这里我处理URL请求并希望执行片段事务 在那里,我调用in

我正在我的应用程序中使用一个基于选项卡的活动,我想用WebView实现一个片段堆栈。在这些WebView中,我希望捕获URL请求并用新的WebView片段替换当前的WebView片段。不幸的是,我总是得到
活动被破坏的异常,但不知道为什么

你能给我一些提示如何解决这个问题吗

我使用以下代码(在代码示例的末尾,您将识别我要替换当前片段的方法
handleUrlRequest
):


我终于明白了。我通过WebViewClient传递了Web实例,在这里我处理URL请求并希望执行片段事务

在那里,我调用
instanceOfWeb.getActivity().getSupportFragmentManager().beginTransaction()
来初始化片段事务

public class FragmentStackSupport extends SherlockFragmentActivity {
int mStackLevel = 1;

public static int THEME = R.style.Theme_Sherlock_Light;

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(THEME); //Used for theme switching in samples
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_stack);


    if (savedInstanceState == null) {
        // Do first time initialization -- add initial fragment.
        Fragment newFragment = Web.newInstance(mStackLevel);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.simple_fragment, newFragment).commit();
    } else {
        mStackLevel = savedInstanceState.getInt("level");
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("level", mStackLevel);
}


public void addFragmentToStack() {
    mStackLevel++;

    // Instantiate a new fragment.
    Fragment newFragment = Web.newInstance(mStackLevel);

    // Add the fragment to the activity, pushing this transaction
    // on to the back stack.
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.simple_fragment, newFragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(null);
    ft.commit();
}



public static class Web extends SherlockFragment {
    int mNum;

    /**
     * Create a new instance of CountingFragment, providing "num"
     * as an argument.
     */
    static Web newInstance(int num) {
        Web f = new Web();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }

    /**
     * When creating, retrieve this instance's number from its arguments.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
    }

    /**
     * The Fragment's UI is just a simple text view showing its
     * instance number.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.hello_world, container, false);
        View wv = v.findViewById(R.id.webview);
        WebView webView = ((WebView)wv);

        webView.loadUrl("http://www.google.at");
        webView.setWebViewClient(new MyWebViewClient());



        return v;
    }
}

public static class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String urlString) {

        handleURLRequest("modal");
        return false;
    }


    private void handleURLRequest(String transitionStyle){

        // here I want to replace the Fragment with a new one.

    }
}

}