如何使用片段在Web视图中向后导航?

时间:2015-10-11 05:00:27

标签: android android-fragments android-webview

当我尝试从片段内部的Web视图向后导航时,我一直收到错误,可能导致此问题的原因是什么?

我的logcat:

    10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime: FATAL EXCEPTION: main
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime: Process: pi.com.ndtc, PID: 28567
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.webkit.WebView.canGoBack()' on a null object reference
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at pi.com.ndtc.SocialActivity.onKeyDown(SocialActivity.java:343)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.KeyEvent.dispatch(KeyEvent.java:2619)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.app.Activity.dispatchKeyEvent(Activity.java:2707)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.support.v7.internal.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:50)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:224)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.support.v7.internal.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:50)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2276)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4020)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3982)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3713)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3874)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2208)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1849)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1840)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2185)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.os.MessageQueue.nativePollOnce(Native Method)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.os.MessageQueue.next(MessageQueue.java:143)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:122)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5257)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
10-10 22:42:23.926 28567-28567/pi.com.ndtc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

logcat指向此行

    at pi.com.ndtc.SocialActivity.onKeyDown(SocialActivity.java:343)

这是:

if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebViewInstagram.canGoBack()) 

这是我用来尝试在网络视图中向后导航的方法:

     @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    WebView myWebViewInstagram = (WebView) findViewById(R.id.webview_instagram);



    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebViewInstagram.canGoBack()) {
        myWebViewInstagram.goBack();

        return true;
    }


    // If it wasn't the Back key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

我的片段:

public class FragmentThree extends Fragment {
public FragmentThree(){
    //Required empty constructor
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
    // Inflate the layout for this fragment
    //return inflater.inflate(R.layout.fragment_two, container, false);

    //Using the "View" to obtain the WebView
    View viewInsta = inflater.inflate(R.layout.fragment_three, container, false);

    //in app WebView
    WebView myInstagramWebView = (WebView) viewInsta.findViewById(R.id.webview_instagram);
    WebSettings webSettings = myInstagramWebView.getSettings();//required for enabling javascript
    webSettings.setJavaScriptEnabled(true); //javascript was disabled by default by webView; i enabled it
    myInstagramWebView.setWebViewClient(new WebViewClient());
    myInstagramWebView.setWebChromeClient(new WebChromeClient());
    final ProgressDialog dialog;
    dialog = new ProgressDialog(getActivity(),R.style.MyDialogTheme);
    //dialog.setMessage("Loading...");
    dialog.show();
    myInstagramWebView.loadUrl("https://instagram.com/ndtcjamaica/");

    myInstagramWebView.setWebViewClient(new WebViewClient() {
                                          public void onPageFinished(WebView myInstagramWebView, String url) {
                                              // DISMISS PROGRESS DIALOG HERE
                                              dialog.dismiss();
                                          }
                                      }
    );

    return viewInsta;
  }
}

1 个答案:

答案 0 :(得分:2)

使用此mWebView应该是您的网页浏览

你必须声明它

private WebView mWebView;

使用onCreate()方法对其进行初始化,您应该很高兴。

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

然后使用下面的代码进行活动

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (mWebView.canGoBack()) {
                    mWebView.goBack();
                } else {
                    finish();
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

如果您在片段

中使用此功能
webView.setOnKeyListener(new OnKeyListener()
{
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        if(event.getAction() == KeyEvent.ACTION_DOWN)
        {
            WebView webView = (WebView) v;

            switch(keyCode)
            {
                case KeyEvent.KEYCODE_BACK:
                    if(webView.canGoBack())
                    {
                        webView.goBack();
                        return true;
                    }
                    break;
            }
        }

        return false;
    }
});

从所有片段调用使用波纹管方法

((YourActivityClassName)getActivity()).yourPublicMethod();