2013-04-10 59 views
0

我有一個錯誤在Android簡單demo.I處理有我的互聯網例行檢查,並用重載調用網頁視圖當我點擊一個menu.Here是我的代碼網頁視圖崩潰點擊

package com.example.test; 

import android.app.Activity; 
import android.content.Context; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

public class MainActivity extends Activity { 
    public static final int DIALOG_NONETWORK = 0; 
    WebView mywebview=null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final WebView mywebview = (WebView) findViewById(R.id.webview); 


     if(!Connection()){ 
      String data = "<html>" + 
        "<body><h1>NO INTERNET</h1></body>" + 
        "</html>"; 
      mywebview.loadData(data, "text/html", "UTF-8"); 
      Log.v("INTERNET","NO"); 

     }else{ 
      mywebview.loadUrl("http://www.google.com"); 
      Log.v("INTERNET","YES"); 

     } 
     mywebview.setWebViewClient(new WebViewClient() { 
      @Override 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       String data = "<html>" + 
         "<body><h1>NO INTERNET</h1></body>" + 
         "</html>"; 
       mywebview.loadData(data, "text/html", "UTF-8"); 

      } 
     }); 

    } 
    public boolean Connection() { 
     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo netInfoMob = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
     NetworkInfo netInfoWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
     if ((netInfoMob != null || netInfoWifi != null) && (netInfoMob.isConnectedOrConnecting() || netInfoWifi.isConnectedOrConnecting())) { 
      return true; 
     } 
     return false; 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
     { 
     switch (item.getItemId()) 
      { 
       case R.id.action_settings: 
        mywebview.reload(); 
        Log.v("INTERNET","RELOAD"); 

        break; 

      } 
        return super.onOptionsItemSelected(item); 
     } 
} 

當我點擊菜單「action_settings」應用程序是在重載fucntion.I剛剛墜毀懷疑它與mywebview.setWebViewClient(new WebViewClient()final WebView mywebview = (WebView) findViewById(R.id.webview);

任何幫助或recomedations辦?

04-10 17:41:03.537: E/ActivityThread(669): Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]e2948 that was originally bound here 
04-10 17:41:03.537: E/ActivityThread(669): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]e2948 that was originally bound here 
04-10 17:41:03.537: E/ActivityThread(669): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969) 
04-10 17:41:03.537: E/ActivityThread(669): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 
04-10 17:41:03.537: E/ActivityThread(669): at android.app.ContextImpl.bindService(ContextImpl.java:1418) 
04-10 17:41:03.537: E/ActivityThread(669): at android.app.ContextImpl.bindService(ContextImpl.java:1407) 
04-10 17:41:03.537: E/ActivityThread(669): at android.content.ContextWrapper.bindService(ContextWrapper.java:473) 
04-10 17:41:03.537: E/ActivityThread(669): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 
04-10 17:41:03.537: E/ActivityThread(669): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 
04-10 17:41:03.537: E/ActivityThread(669): at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116) 
04-10 17:41:03.537: E/ActivityThread(669): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249) 
04-10 17:41:03.537: E/ActivityThread(669): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856) 
04-10 17:41:03.537: E/ActivityThread(669): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 
04-10 17:41:03.537: E/ActivityThread(669): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) 
04-10 17:41:03.537: E/ActivityThread(669): at android.os.AsyncTask$2.call(AsyncTask.java:287) 
04-10 17:41:03.537: E/ActivityThread(669): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
04-10 17:41:03.537: E/ActivityThread(669): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
04-10 17:41:03.537: E/ActivityThread(669): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
04-10 17:41:03.537: E/ActivityThread(669): at java.lang.Thread.run(Thread.java:856) 
04-10 17:41:03.557: E/StrictMode(669): null 
04-10 17:41:03.557: E/StrictMode(669): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]e2948 that was originally bound here 
04-10 17:41:03.557: E/StrictMode(669): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969) 
04-10 17:41:03.557: E/StrictMode(669): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 
04-10 17:41:03.557: E/StrictMode(669): at android.app.ContextImpl.bindService(ContextImpl.java:1418) 
04-10 17:41:03.557: E/StrictMode(669): at android.app.ContextImpl.bindService(ContextImpl.java:1407) 
04-10 17:41:03.557: E/StrictMode(669): at android.content.ContextWrapper.bindService(ContextWrapper.java:473) 
04-10 17:41:03.557: E/StrictMode(669): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 
04-10 17:41:03.557: E/StrictMode(669): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 
04-10 17:41:03.557: E/StrictMode(669): at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116) 
04-10 17:41:03.557: E/StrictMode(669): at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249) 
04-10 17:41:03.557: E/StrictMode(669): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856) 
04-10 17:41:03.557: E/StrictMode(669): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 
04-10 17:41:03.557: E/StrictMode(669): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) 
04-10 17:41:03.557: E/StrictMode(669): at android.os.AsyncTask$2.call(AsyncTask.java:287) 
04-10 17:41:03.557: E/StrictMode(669): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
04-10 17:41:03.557: E/StrictMode(669): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
04-10 17:41:03.557: E/StrictMode(669): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
04-10 17:41:03.557: E/StrictMode(669): at java.lang.Thread.run(Thread.java:856) 
04-10 17:41:03.657: E/ActivityThread(669): Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]ecbf8 that was originally bound here 
04-10 17:41:03.657: E/ActivityThread(669): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]ecbf8 that was originally bound here 
04-10 17:41:03.657: E/ActivityThread(669): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969) 
04-10 17:41:03.657: E/ActivityThread(669): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 
04-10 17:41:03.657: E/ActivityThread(669): at android.app.ContextImpl.bindService(ContextImpl.java:1418) 
04-10 17:41:03.657: E/ActivityThread(669): at android.app.ContextImpl.bindService(ContextImpl.java:1407) 
04-10 17:41:03.657: E/ActivityThread(669): at android.content.ContextWrapper.bindService(ContextWrapper.java:473) 
04-10 17:41:03.657: E/ActivityThread(669): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 
04-10 17:41:03.657: E/ActivityThread(669): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 
04-10 17:41:03.657: E/ActivityThread(669): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) 
04-10 17:41:03.657: E/ActivityThread(669): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850) 
04-10 17:41:03.657: E/ActivityThread(669): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 
04-10 17:41:03.657: E/ActivityThread(669): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) 
04-10 17:41:03.657: E/ActivityThread(669): at android.os.AsyncTask$2.call(AsyncTask.java:287) 
04-10 17:41:03.657: E/ActivityThread(669): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
04-10 17:41:03.657: E/ActivityThread(669): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
04-10 17:41:03.657: E/ActivityThread(669): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
04-10 17:41:03.657: E/ActivityThread(669): at java.lang.Thread.run(Thread.java:856) 
04-10 17:41:03.697: E/StrictMode(669): null 
04-10 17:41:03.697: E/StrictMode(669): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection [email protected]ecbf8 that was originally bound here 
04-10 17:41:03.697: E/StrictMode(669): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969) 
04-10 17:41:03.697: E/StrictMode(669): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 
04-10 17:41:03.697: E/StrictMode(669): at android.app.ContextImpl.bindService(ContextImpl.java:1418) 
04-10 17:41:03.697: E/StrictMode(669): at android.app.ContextImpl.bindService(ContextImpl.java:1407) 
04-10 17:41:03.697: E/StrictMode(669): at android.content.ContextWrapper.bindService(ContextWrapper.java:473) 
04-10 17:41:03.697: E/StrictMode(669): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 
04-10 17:41:03.697: E/StrictMode(669): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 
04-10 17:41:03.697: E/StrictMode(669): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) 
04-10 17:41:03.697: E/StrictMode(669): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850) 
04-10 17:41:03.697: E/StrictMode(669): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 
04-10 17:41:03.697: E/StrictMode(669): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) 
04-10 17:41:03.697: E/StrictMode(669): at android.os.AsyncTask$2.call(AsyncTask.java:287) 
04-10 17:41:03.697: E/StrictMode(669): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
04-10 17:41:03.697: E/StrictMode(669): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
04-10 17:41:03.697: E/StrictMode(669): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
04-10 17:41:03.697: E/StrictMode(669): at java.lang.Thread.run(Thread.java:856) 
+0

從LogCat發佈您的堆棧跟蹤。 – kabuko 2013-04-10 17:37:08

+0

是的,忘記了。現在編輯。 – Theodoros80 2013-04-10 17:42:37

+0

嘗試刪除null,僅Webview mywebview; – k0sh 2013-04-10 17:44:42

回答

0

嗯,我懷疑

final WebView mywebview = (WebView) findViewById(R.id.webview); 

是問題。

我改成了

mywebview = (WebView) findViewById(R.id.webview); 

和工作就像一個魅力。

無論如何,希望它能幫助別人。