尝试在空对象引用上调用虚方法'boolean android.webkit.WebView.canGoBack()'

时间:2016-07-19 16:31:50

标签: android nullpointerexception android-webview

我的应用程序加载了包含webView的片段,但是当我尝试按下后退按钮时。它崩溃说网络是空的。我不知道为什么。请帮忙。我可以使用WebViewFragment中的访问器

MainActivity

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.webkit.WebView;

public class MainActivity extends FragmentActivity {

    WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        WebViewFragment webViewFragment = new WebViewFragment();
        fragmentTransaction.replace(R.id.mainFragment, webViewFragment);
        fragmentTransaction.commit();
    }

    @Override
    public void onBackPressed() {

            if(mWebView.canGoBack()) {
                mWebView.goBack();

            }
    else
        {
            super.onBackPressed();
        }
    }

}

WebViewFragment

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewFragment extends Fragment {

    private WebView mWebView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);

        mWebView = (WebView) view.findViewById(R.id.webView);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("https://www.google.co.uk/");
        mWebView.setWebViewClient(new WebViewClient());

        return view;
    }

}

1 个答案:

答案 0 :(得分:0)

您的mWebViewWebViewFragment.java内定义,mWebView中的MainActivity.java是另一个对象,值为空。因此mWebView.canGoBack()会出错,因为mWebView是一个空对象。

您应该修改MainActivity.java中的onBackPressed(),例如:

@Override
public void onBackPressed() {
    Fragment webview = getSupportFragmentManager().findFragmentByTag("webview");

    if ( (webview instanceof WebViewFragment) && WebViewFragment.canGoBack() ) {
            WebViewFragment.goBack();
    } else {
        super.onBackPressed();
    }
}

此外,将唯一标识片段标记添加到MainActivity.java中第fragmentTransaction.replace(...)行的第3个参数,例如“网页视图”:

fragmentTransaction.replace(R.id.mainFragment, webViewFragment, "webview");

然后,在mWebView内使class WebViewFragment静态:

private static WebView mWebView;

确保您在class WebViewFragment

中定义了这2个静态方法
public static boolean canGoBack(){
    return myWebView.canGoBack();
}

public static void goBack(){
    myWebView.goBack();
}