我是Android开发的初学者,这是我的项目: 我想回到WebView中的上一页。
当我运行代码时,这些是错误: 1. 错误:(67,5)错误:方法不会覆盖或实现超类型的方法 2. 错误:(75,18)错误:找不到符号方法onBackPressed() 3. 错误:(70,13)错误:找不到符号变量视图
package com.veereshc.veer.resultapp;
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.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
import com.veereshc.veer.resultapp.dummy.DummyContent;
public class WebpageDetailFragment extends Fragment {
public static final String ARG_ITEM_ID = "item_id";
private DummyContent.DummyItem mItem;
public WebpageDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_webpage_detail, container, false);
if (mItem != null) {
WebView view = (WebView) rootView.findViewById(R.id.detail_area);
view.setWebViewClient(new WebViewClient());
view.loadUrl(mItem.url);
}
return rootView;
}
@Override
public void onBackPressed(){
if(view.canGoBack()){
view.goBack();
}
else
{
super.onBackPressed();
}
}
}
答案 0 :(得分:1)
如果在WebView中按下后退,返回上一页有两种方法:
首先,覆盖onBackPressed()
:
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
if (webview.canGoBack()) {
webview.goBack();
return;
} else {
super.onBackPressed();
}
}
第二次,覆盖onKeyDown
:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
答案 1 :(得分:1)
像这样使用onBackPressed方法
@Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
答案 2 :(得分:0)
您没有在任何地方定义变量view
。
您可以在另一个函数的范围内定义它,因此您无法在onBackPressed()
实际上,在处理WebView时,您可以设置一个全局变量并使用它:
public class WebpageDetailFragment extends Fragment {
public static final String ARG_ITEM_ID = "item_id";
private WebView globalView;
private DummyContent.DummyItem mItem;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_webpage_detail, container, false);
if (mItem != null) {
WebView view = (WebView) rootView.findViewById(R.id.detail_area);
view.setWebViewClient(new WebViewClient());
view.loadUrl(mItem.url);
}
globalView = view;
return rootView;
}
//Implement onKeyDown and check if Back
@override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (getFragmentManager().getBackStackEntryCount() == 0) {
super.onBackPressed();
} else {
if(globalView.canGoBack()){
globalView.goBack();
}
}
return true;
}
return false;
}
}