0

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

我用谷歌搜索并搜索了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)
4

1 回答 1

2

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

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

WebView = (WebView) findViewById(R.id.webView);
于 2013-06-27T17:26:53.327 回答