简体   繁体   English

Android WebApp强制关闭后退按钮

[英]Android WebApp force closes on back button

I have a web app I'm making to run off a CMS that is using jQuery Mobile to make mobile compatible page versions. 我有一个网络应用程序,我正在使用一个使用jQuery Mobile制作移动兼容页面版本的CMS。 The base log in page is stored locally on the phone. 基本登录页面本地存储在手机上。

public class WebAppActivity extends Activity {
WebView webView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    // Point to the content view defined in XML

    setContentView(R.layout.main);

    // Configure the webview setup in the xml layout

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

    WebSettings webSettings = webView.getSettings();

    // Yes, we want javascript, pls.

    webSettings.setJavaScriptEnabled(true);


    webView.loadUrl("file:///android_asset/www/webapp.html");



webView.setWebViewClient(new WebViewClient() { 
    @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url){
         view.loadUrl(url);
         return true;
     }
 });


}



@Override
 public void onBackPressed()
{
    if(webView.canGoBack())
    {
        webView.goBack();
    }else{
        super.onBackPressed();
    }
}

} }

and the the Logcat says : 而Logcat说:

03-29 15:31:47.354: E/AndroidRuntime(430): FATAL EXCEPTION: main
03-29 15:31:47.354: E/AndroidRuntime(430): java.lang.NullPointerException
03-29 15:31:47.354: E/AndroidRuntime(430):  at com.package.mobile.android.webapp.onBackPressed(WebApp.java:223)
03-29 15:31:47.354: E/AndroidRuntime(430):  at android.app.Activity.onKeyUp(Activity.java:1898)
03-29 15:31:47.354: E/AndroidRuntime(430):  at android.view.KeyEvent.dispatch(KeyEvent.java:1280)
03-29 15:31:47.354: E/AndroidRuntime(430):  at android.app.Activity.dispatchKeyEvent(Activity.java:2078)
03-29 15:31:47.354: E/AndroidRuntime(430):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663)
03-29 15:31:47.354: E/AndroidRuntime(430):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560)
03-29 15:31:47.354: E/AndroidRuntime(430):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535)
03-29 15:31:47.354: E/AndroidRuntime(430):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
03-29 15:31:47.354: E/AndroidRuntime(430):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 15:31:47.354: E/AndroidRuntime(430):  at android.os.Looper.loop(Looper.java:123)
03-29 15:31:47.354: E/AndroidRuntime(430):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-29 15:31:47.354: E/AndroidRuntime(430):  at java.lang.reflect.Method.invokeNative(Native Method)
03-29 15:31:47.354: E/AndroidRuntime(430):  at java.lang.reflect.Method.invoke(Method.java:507)
03-29 15:31:47.354: E/AndroidRuntime(430):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-29 15:31:47.354: E/AndroidRuntime(430):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-29 15:31:47.354: E/AndroidRuntime(430):  at dalvik.system.NativeStart.main(Native Method)

I've been trying to get around this for the past week or so, it seems as if the webview isn't building a history to go back through. 在过去一周左右的时间里,我一直试图解决这个问题,似乎webview并没有建立历史可以追溯到过去。

try this : 尝试这个 :

public boolean onKeyDown(int keyCode, KeyEvent event) {  
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {  
        webView.goBack();  
        return true;  
    }  
    return super.onKeyDown(keyCode, event);  
}  

The NullPointerException is because your webView is a local variable in onCreate , and the webView class member variable is left unassigned. NullPointerException是因为您的webViewonCreate的局部变量,而webView类成员变量是未分配的。

Off-topic: I won't go into a long tirade about purely WebView-based apps, but let's just say you shouldn't do it. 偏离主题: 我不会对纯粹的基于WebView的应用程序进行长篇大论,但我们只是说你不应该这样做。 And if you absolutely have to for some reason or another, then you should make sure to handle orientation changes. 如果您出于某种原因绝对不得不这样做,那么您应该确保处理方向更改。

you need to override the backbutton default behaviour, this is achieved by 你需要覆盖后退按钮的默认行为,这是通过实现的

       @Override
public void onBackPressed() {

    return;
}

in the main activity of your application 在您的应用程序的主要活动中

按下后退按钮你应该拨打电话

finish();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM