尝试使用选项卡式布局在SherlockFragment中创建WebView,但会出现错误。之前有工作代码,但试图更改为onKeyDown现在可以工作得到错误。谢谢大家。任何帮助都是极好的。感谢
Code Java:
WebView myWebView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_4, container, false);
}
String mURL = "https://www.facebook.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("DetailFragment", "onCreate()");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.v("DetailFragment", "onActivityCreated()");
if (savedInstanceState != null) {
mURL = savedInstanceState.getString("currentURL", "");
}
if (!mURL.trim().equalsIgnoreCase("")) {
myWebView = (WebView) getView().findViewById(R.id.webView);
myWebView.loadUrl(mURL.trim());
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
getView().setOnKeyListener( new OnKeyListener()
{
@Override
public boolean onKey( View v, int keyCode, KeyEvent event ) {
return myOnKeyDown(keyCode);
}
} );
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("currentURL", mURL);
}
public void setURLContent(String URL) {
mURL = URL;
}
public void updateURLContent(String URL) {
mURL = URL;
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl(mURL.trim());
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
}
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context fragment_4) {
mContext = fragment_4;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("https://www.facebook.com")) {
// This is my web site, so do not override; let my WebView
// load the page
return false;
}
return false;
}
}
public boolean myOnKeyDown(int key_code) {
if ((key_code == KeyEvent.KEYCODE_BACK)
&& myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return false;
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_4, menu);
return;
}
}
XML:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
错误:
07-27 11:29:57.299: E/AndroidRuntime(6054): FATAL EXCEPTION: main
07-27 11:29:57.299: E/AndroidRuntime(6054): java.lang.NullPointerException
07-27 11:29:57.299: E/AndroidRuntime(6054): at com.bottlecapp.appstest.Fragment_4.onActivityCreated(Fragment_4.java:46)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1486)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.view.Choreographer.doFrame(Choreographer.java:531)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.os.Handler.handleCallback(Handler.java:725)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.os.Handler.dispatchMessage(Handler.java:92)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.os.Looper.loop(Looper.java:137)
07-27 11:29:57.299: E/AndroidRuntime(6054): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-27 11:29:57.299: E/AndroidRuntime(6054): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 11:29:57.299: E/AndroidRuntime(6054): at java.lang.reflect.Method.invoke(Method.java:511)
07-27 11:29:57.299: E/AndroidRuntime(6054): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-27 11:29:57.299: E/AndroidRuntime(6054): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-27 11:29:57.299: E/AndroidRuntime(6054): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
班级成员myWebView
永远不会被初始化。在onActivityCreated里面做:
if (!mURL.trim().equalsIgnoreCase("")) {
myWebView = (WebView) getView().findViewById(R.id.webviewId);
myWebView.loadUrl(mURL.trim());
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
}
编辑:
更改
myWebView = (WebView) getView().findViewById(R.id.fragment_4);
与
myWebView = (WebView) getView().findViewById(R.id.webView);
编辑2:在onActivityCreated内部
getView().setOnKeyListener( new OnKeyListener()
{
@Override
public boolean onKey( View v, int keyCode, KeyEvent event ) {
return myOnKeyDown(keyCode);
}
} );
public boolean myOnKeyDown(int key_code) {
if ((key_code == KeyEvent.KEYCODE_BACK)
&& myWebView.canGoBack()) {
myWebView.goBack();
return false;
}
return true;
}
}