当我使用事件onPause和/或onResume时,-crash-不起作用。

public class xx extends Fragment {
    private WebView wv;
    private WebView wv2;
    public static final String TAG = "xx";


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.livewithchat, container, false);
        WebView wv = (WebView) rootView.findViewById(R.id.webView);
        WebSettings webSettings = wv.getSettings();
        webSettings.setJavaScriptEnabled(true);
        wv.loadUrl("XX");

        /* Chat */
        WebView wv2 = (WebView) rootView.findViewById(R.id.webView2);
        wv2.getSettings().setJavaScriptEnabled(true);
        wv2.getSettings().setSaveFormData(true);
        wv2.getSettings().setBuiltInZoomControls(false);
        wv2.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
            }
        });
        wv2.setWebViewClient(
                new WebViewClient(){
                    public boolean shouldOverrideUrlLoading(WebView view, String url){
                        view.loadUrl(url);
                        return false;
                    }
                });
        wv2.loadUrl("XX");
        wv2.addJavascriptInterface(new WebSocketFactory(wv2), "WebSocketFactory");


        return rootView;
    }

    @Override
    public void onResume()
    {
        super.onResume();
        wv.onResume();
        wv2.onResume();
    }
    @Override
    public void onPause()
    {
        super.onPause();
        wv.onPause();
        wv2.onPause();
    }

    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed(); // Ignore SSL certificate errors
    }
}


Logcat:

07-05 20:37:46.313  11757-11757/com.clemb.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.clemb.test, PID: 11757
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.onResume()' on a null object reference
            at com.clemb.test.livewithchat.onResume(livewithchat.java:68)
            at android.app.Fragment.performResume(Fragment.java:2192)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:956)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
            at android.app.BackStackRecord.run(BackStackRecord.java:833)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1580)
            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:451)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5696)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)


我的片段:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="228dp"
        android:id="@+id/webView" />

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView2" />
</LinearLayout>


我已经尝试更改"private wv",但是并不能解决问题。

我在其他活动(我想创建一个Web应用程序)中使用其他webview,这是同样的问题。

我在StackOverflow中进行了搜索,但没有找到解决方案。

最佳答案

这是因为wvwv2为空,您将它们初始化为局部变量而不是使用实例变量,请更改:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    WebView wv = (WebView) rootView.findViewById(R.id.webView);
    ...

    /* Chat */
    WebView wv2 = (WebView) rootView.findViewById(R.id.webView2);




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    wv = (WebView) rootView.findViewById(R.id.webView);
    ...

    /* Chat */
    wv2 = (WebView) rootView.findViewById(R.id.webView2);


这样,您将使用实例变量,这将阻止您获取NullPointerExceptions。

更新

检查这篇文章,它对局部变量和实例变量https://stackoverflow.com/a/2088370/1333516之间的区别有很好的解释

10-05 17:59