我是android开发新手,我试图覆盖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();
}
}
}
答案 0 :(得分:0)
仅因为您的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();
}
}
}
答案 1 :(得分:0)
在onCreate
中你有:
WebView view = (WebView) findViewById(R.id.webView);
您在顶部定义的WebView尚未初始化,并且是onBackPressed
方法正在使用的WebView,要解决此问题,您需要做的就是在初始化变量之前删除WebView
在onCreate
:
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");
}
答案 2 :(得分:0)
实际上你不能直接在片段里面做。可以在FragmentActivity中覆盖onBackPressed。你能做的是:
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();
}
}