onBackPressed()使我的webview应用程序崩溃



我是安卓开发的新手,我曾尝试覆盖onBackPress()来实现webView.GoBack()。但按回车键后,我的应用程序就崩溃了。这是我的MainActivity.java代码。我做错什么了吗??

public class MainActivity extends AppCompatActivity {
private WebView webview;
    /** Called when the activity is first created. */
    public void onBackPressed (){
        if (webview.isFocused() && webview.canGoBack()) {
                webview.goBack();
        }
        else {
               super.onBackPressed();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView view = (WebView) findViewById(R.id.webView);
        view.getSettings().setJavaScriptEnabled(true);
        view.setWebViewClient(new MyCustomWebViewClient());
        view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        view.loadUrl("http://url");

    }

    private class MyCustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
           view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            //hide loading image
            findViewById(R.id.imageLoading1).setVisibility(View.GONE);
            //show webview
            findViewById(R.id.webView).setVisibility(View.VISIBLE);
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            if (view.canGoBack()) {
                view.goBack();
            } else {
                view.loadUrl("file:///android_asset/index.html");
            }
            Toast.makeText(getBaseContext(), description, Toast.LENGTH_LONG).show();
        }
    }

}

因为您的webView为空。你没有在任何地方引用它。修改了您的代码。

public class MainActivity extends AppCompatActivity { 
private WebView view;
    /** Called when the activity is first created. */ 
    public void onBackPressed (){ 
        if (view.isFocused() && view.canGoBack()) {
            view.goBack();
        } 
        else { 
            super.onBackPressed(); 
        } 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        view = (WebView) findViewById(R.id.webView);
        view.getSettings().setJavaScriptEnabled(true);
        view.setWebViewClient(new MyCustomWebViewClient());
        view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        view.loadUrl("http://url");

    } 

    private class MyCustomWebViewClient extends WebViewClient {
        @Override 
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
           view.loadUrl(url);
            return true; 
        } 

        @Override 
        public void onPageFinished(WebView view, String url) {
            //hide loading image 
            findViewById(R.id.imageLoading1).setVisibility(View.GONE);
            //show webview 
            findViewById(R.id.webView).setVisibility(View.VISIBLE);
        } 

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            if (view.canGoBack()) {
                view.goBack();
            } else { 
                view.loadUrl("file:///android_asset/index.html");
            } 
            Toast.makeText(getBaseContext(), description, Toast.LENGTH_LONG).show();
        } 
    } 
} 

onCreate中,您有:

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

您在顶部定义的WebView尚未初始化,并且是onBackPressed方法正在使用的WebView。要解决此问题,您只需要在初始化onCreate:中的变量之前删除WebView

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    view = (WebView) findViewById(R.id.webView);
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new MyCustomWebViewClient());
    view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    view.loadUrl("http://url");

}

实际上,您不能直接在片段内部执行操作。onBackPressed可以在FragmentActivity中被覆盖。你能做的是:

1.Override the onBackPressed inside the activity.
2.When the onBackPressed is called, check if the instance of the current fragment is the instance showing the webview.
3.If it is, ask the fragment if the webview can go back.
4.If it is not, call the super or whatever you need
 @Override
 public void onBackPressed() {
   Fragment webview = getSupportFragmentManager().findFragmentByTag("webview");
   if (webview instanceof MyWebViewFragment) {
          boolean goback = ((MyWebViewFragment)webview).canGoBack();
          if (!goback)
            super.onBackPressed();
   }
 }

相关内容

  • 没有找到相关文章

最新更新