-2

我在我的应用程序中添加了一个带有后退、刷新、前进和关于选项的侧面选项菜单,但是每当我单击这些按钮中的任何一个时,我的应用程序都会停止工作,请帮助我。

如果可能,请突出显示错误的代码部分..

代码:

public class MainActivity extends Activity {
WebView myWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

    // Let's display the progress in the activity title bar, like the
    // browser app does.
    getWindow().requestFeature(Window.FEATURE_PROGRESS);

    WebView webview = new WebView(this);
    setContentView(webview);
    webview.getSettings().setJavaScriptEnabled(true);

    webview.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long contentLength) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
        }
    });

    webview.setOnKeyListener(new View.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;
        }
    });

    final Activity activity = this;
    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            // Activities and WebViews measure progress with different scales.
            // The progress meter will automatically disappear when we reach 100%
            activity.setProgress(progress * 1000);
        }
    });

    webview.setWebViewClient(new WebViewClient() {

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            //Users will be notified in case there's an error (i.e. no internet connection)
            Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
        }
    });
    //This will load the webpage that we want to see
    webview.loadUrl("http://www.xxxxx.com");



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu); // Add menu items, second value is the id, use this in the onCreateOptionsMenu
    menu.add(0, 1, 0, "Back");
    menu.add(0, 2, 0, "Refresh");
    menu.add(0, 3, 0, "Forward");
    menu.add(0, 4, 0, "About");
    return true; // End of menu configuration
}
public boolean onOptionsItemSelected(MenuItem item){ // Called when you tap a menu item

    switch (item.getItemId()){
        case 1: //If the ID equals 1, go back
            if (myWebView.canGoBack()){
            myWebView.goBack();
            return true;
                }
        case 2 : //If the ID equals 2, refresh
            myWebView.reload();
            return true;
        case 3: //If the ID equals 3, go forward
            if (myWebView.canGoForward()){
            myWebView.goForward();
            return true;
            }
        case 4: //aha showing about us
            myWebView.loadUrl("http://example.com/about.php");
    }
    return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { // Enables browsing to previous pages with the hardware back button
    WebView webView = new WebView(this);
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { // Check if the key event was the BACK key and if there's history
        webView.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);
}
}

这是日志:

08-04 12:38:25.319        37-37/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
08-04 12:38:28.687    3589-3589/com.example D/AndroidRuntime: Shutting down VM
08-04 12:38:28.687    3589-3589/com.example W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-04 12:38:28.778    3589-3589/com.example E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.NullPointerException
    at com.example.MainActivity.onOptionsItemSelected(MainActivity.java:125)
    at android.app.Activity.onMenuItemSelected(Activity.java:2548)
    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
    at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:156)
    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
    at android.widget.AbsListView$1.run(AbsListView.java:3423)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
08-04 12:38:28.817      322-542/system_process W/ActivityManager: Force finishing activity com.example/.MainActivity
08-04 12:38:28.837      322-542/system_process W/WindowManager: Failure taking screenshot for (328x583) to layer 21035
08-04 12:38:29.287       37-162/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
08-04 12:38:29.425      322-337/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{411a3cd0 u0 com.example/.MainActivity}
08-04 12:38:29.747      708-723/com.android.exchange D/ExchangeService: Received deviceId from Email app: null
08-04 12:38:29.747      708-723/com.android.exchange D/ExchangeService: !!! deviceId unknown; stopping self and retrying
08-04 12:38:30.047      457-457/com.android.launcher I/Choreographer: Skipped 35 frames!  The application may be doing too much work on its main thread.
08-04 12:38:30.337        37-37/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
08-04 12:38:34.848      708-724/com.android.exchange D/ExchangeService: !!! EAS     ExchangeService, onCreate
08-04 12:38:34.848      708-708/com.android.exchange D/ExchangeService: !!! EAS ExchangeService, onStartCommand, startingUp = false, running = false
08-04 12:38:34.858      322-560/system_process W/ActivityManager: Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
08-04 12:38:34.858      708-898/com.android.exchange D/ExchangeService: !!! Email application not found; stopping self
08-04 12:38:34.880      322-669/system_process W/ActivityManager: Unable to start service Intent { act=com.android.email.ACCOUNT_INTENT } U=0: not found
08-04 12:38:34.887      708-708/com.android.exchange D/ExchangeService: !!! EAS ExchangeService, onStartCommand, startingUp = true, running = false
08-04 12:38:34.918      708-708/com.android.exchange E/ActivityThread: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d4ceb0 that was originally bound here
    android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d4ceb0 that was originally bound here
    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
    at android.app.ContextImpl.bindService(ContextImpl.java:1418)
    at android.app.ContextImpl.bindService(ContextImpl.java:1407)
    at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
    at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
    at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
    at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
    at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
    at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
    at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
    at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:856)
08-04 12:38:35.021      708-708/com.android.exchange E/StrictMode: null
    android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d4ceb0 that was originally bound here
    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
    at android.app.ContextImpl.bindService(ContextImpl.java:1418)
    at android.app.ContextImpl.bindService(ContextImpl.java:1407)
    at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
    at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
    at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
    at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
    at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
    at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
    at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
    at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:856)
08-04 12:38:35.021      322-333/system_process W/ActivityManager: Unbind failed: could not find connection for android.os.BinderProxy@411d8540
08-04 12:38:35.047      708-708/com.android.exchange E/ActivityThread: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ce0338 that was originally bound here
    android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ce0338 that was originally bound here
    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
    at android.app.ContextImpl.bindService(ContextImpl.java:1418)
    at android.app.ContextImpl.bindService(ContextImpl.java:1407)
    at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
    at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
    at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
    at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
    at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
    at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
    at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:856)
08-04 12:38:35.047      708-708/com.android.exchange E/StrictMode: null
    android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ce0338 that was originally bound here
    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
    at android.app.ContextImpl.bindService(ContextImpl.java:1418)
    at android.app.ContextImpl.bindService(ContextImpl.java:1407)
    at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
    at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
    at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
    at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
    at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
    at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
    at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:856)
08-04 12:38:35.068      322-474/system_process W/ActivityManager: Unbind failed: could not find connection for android.os.BinderProxy@411d82a8
08-04 12:38:35.118      708-711/com.android.exchange D/dalvikvm: GC_CONCURRENT freed 464K, 20% free 2503K/3112K, paused 80ms+7ms, total 189ms
08-04 12:38:42.870      322-337/system_process W/ActivityManager: Activity destroy timeout for ActivityRecord{411a3cd0 u0 com.example/.MainActivity}
4

1 回答 1

1

你忘了初始化myWebView

onCreate我想你应该在你的方法中添加以下代码:

myWebView = 网络视图;

于 2013-08-04T12:52:49.923 回答