按下后退键的Android app强制关闭

时间:2013-06-27 17:23:45

标签: android android-webview

我正在开发一个简单的网络应用。几天前,它与后退按钮一起工作得很好。但我最近添加了admob,除了模拟器和设备中的后退按钮外,一切正常。它说出乎意料地停了下来。

我用Google搜索并搜索了stackoverflow,找到了一个同名的问题,但答案不正确。所以这不是一个重复的问题。

这是mainActivity.java

public class MainActivity extends Activity {
    private WebView WebView;

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

        setContentView(R.layout.main);  
        WebView WebView = (WebView) findViewById(R.id.webView);
        WebView.loadUrl("file:///android_asset/index.html");
        WebView.getSettings().setJavaScriptEnabled(true);
        WebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        }); 
    }

    // For Options Menu
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.about:
                startActivity(new Intent(this, About.class));
                return true;
            case R.id.exit:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

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

首先,我无法在我的WebView中放置广告,因此此XML代码与之前的版本不同(当后退密钥正常工作时)。我有从不同来源粘贴的副本来修复它,但现在广告和WebView完全正常工作,除了后退键。

这是main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.02" />

    <com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="my ad id"
        ads:loadAdOnCreate="true" >
    </com.google.ads.AdView>

</LinearLayout>

从logcat

登录
06-27 23:10:40.000: V/WebRequest(10155): WebRequest::WebRequest, setPriority = 1
06-27 23:10:40.030: V/chromium(10155): external/chromium/net/disk_cache/hostres_plugin_bridge.cc:52: [0627/231040:INFO:hostres_plugin_bridge.cc(52)] StatHubCreateHostResPlugin initializing...
06-27 23:10:40.030: V/chromium(10155): external/chromium/net/disk_cache/hostres_plugin_bridge.cc:57: [0627/231040:INFO:hostres_plugin_bridge.cc(57)] StatHubCreateHostResPlugin lib loaded
06-27 23:10:40.040: V/chromium(10155): external/chromium/net/disk_cache/hostres_plugin_bridge.cc:63: [0627/231040:INFO:hostres_plugin_bridge.cc(63)] StatHubCreateHostResPlugin plugin connected
06-27 23:10:40.040: V/chromium(10155): external/chromium/net/http/http_cache.cc:1167: [0627/231040:INFO:http_cache.cc(1167)] HttpCache::OnBackendCreated HostStat created
06-27 23:10:40.040: E/chromium(10155): external/chromium/net/disk_cache/stat_hub.cc:213: [0627/231040:ERROR:stat_hub.cc(213)] StatHub::Init - App com.example.mywebapp isn't supported.
06-27 23:10:40.050: E/chromium(10155): external/chromium/net/disk_cache/stat_hub.cc:213: [0627/231040:ERROR:stat_hub.cc(213)] StatHub::Init - App com.example.mywebapp isn't supported.
06-27 23:10:40.050: W/chromium(10155): external/chromium/net/disk_cache/backend_impl.cc:1829: [0627/231040:WARNING:backend_impl.cc(1829)] Destroying invalid entry.
06-27 23:10:41.590: I/QC_RIL_OEM_HOOK(10155): QcRilOemHook Service Created Successfully
06-27 23:10:41.590: V/QC_RIL_OEM_HOOK(10155): sendQcRilOemHookMsg: Outgoing Data is 5155414c434f4d4d0300080006000000726d6e657430
06-27 23:10:45.660: W/dalvikvm(10155): threadid=1: thread exiting with uncaught exception (group=0x40aa2390)
06-27 23:10:45.660: E/AndroidRuntime(10155): FATAL EXCEPTION: main
06-27 23:10:45.660: E/AndroidRuntime(10155): java.lang.NullPointerException
06-27 23:10:45.660: E/AndroidRuntime(10155):    at com.example.mywebapp.MainActivity.onKeyDown(MainActivity.java:57)
06-27 23:10:45.660: E/AndroidRuntime(10155):    at android.view.KeyEvent.dispatch(KeyEvent.java:2551)
06-27 23:10:45.660: E/AndroidRuntime(10155):    at android.app.Activity.dispatchKeyEvent(Activity.java:2329)
06-27 23:10:45.660: E/AndroidRuntime(10155):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
06-27 23:10:45.660: E/AndroidRuntime(10155):    at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3342)
06-27 23:10:45.660: E/AndroidRuntime(10155):    at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3315)
06-27 23:10:45.660: E/AndroidRuntime(10155):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2460)
06-27 23:10:45.660: E/AndroidRuntime(10155):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 23:10:45.660: E/AndroidRuntime(10155):    at android.os.Looper.loop(Looper.java:137)
06-27 23:10:45.660: E/AndroidRuntime(10155):    at android.app.ActivityThread.main(ActivityThread.java:4456)
06-27 23:10:45.660: E/AndroidRuntime(10155):    at java.lang.reflect.Method.invokeNative(Native Method)
06-27 23:10:45.660: E/AndroidRuntime(10155):    at java.lang.reflect.Method.invoke(Method.java:511)
06-27 23:10:45.660: E/AndroidRuntime(10155):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
06-27 23:10:45.660: E/AndroidRuntime(10155):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
06-27 23:10:45.660: E/AndroidRuntime(10155):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

WebView为null,因为在onCreate函数中,您创建了一个名为WebView的局部变量,因此您的赋值不会分配给实例变量。变化

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

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