2011-02-24 145 views
0

我用下面的代碼來下載文件。它運行良好,但是當我單擊下載按鈕時,出現以下錯誤:「應用程序mibooks意外停止。」Android應用程序意外關閉

我該如何在我的代碼中解決這個問題?

package mds.mibooks; 

import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.webkit.DownloadListener; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Button; 


public class mibooks extends Activity { 
    WebView mWebView; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     final Button button = (Button) findViewById(R.id.button1); 
     button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       setContentView(R.layout.webview); 

       WebView myWebView = (WebView) findViewById(R.id.webView1); 
       WebSettings webSettings = myWebView.getSettings(); 
       WebView myWebView1 = (WebView) findViewById(R.id.webView1); 
       myWebView1.setWebViewClient(new WebViewClient()); 
       webSettings.setJavaScriptEnabled(true); 


       /* WebView webView = (WebView) findViewById(R.id.webView1); 
       webView.addJavascriptInterface(new JavaScriptInterface(this), "Android"); 
       */ 

      // String summary = "<html><body>You scored <b>192</b> points.</body></html>"; 
       // myWebView1.loadData(summary, "text/html", "utf-8"); 
       myWebView1.loadUrl("http://www.mibooks.com/beta/"); 


       myWebView1.setDownloadListener(new DownloadListener() { 
         public void onDownloadStart(String url, String userAgent, 
           String contentDisposition, String mimetype, 
           long contentLength) { 
          Intent intent = new Intent(Intent.ACTION_VIEW); 
          intent.setData(Uri.parse("www.google.com")); 
        intent.setType("*zip*"); 
          startActivity(intent); 

         } 
        }); 

      }  
     }); 
    } 
} 
+2

請提供堆棧跟蹤。 – Mudassir 2011-02-24 04:21:28

+1

我希望你在使用Eclipse。切換到DDMS的角度,然後點擊Logcat,在這裏你會看到所有的調試,警告和錯誤日誌。每當Android拋出任何異常時,它都會在DDMS中生成錯誤日誌,告訴我們這個問題。 – Mudassir 2011-02-24 04:32:21

+0

是的,你是對的..它顯示了許多行我無法弄清楚。最後一行是:[02-24 10:21:54.381:DEBUG/SntpClient(67):請求時間失敗:java.net.SocketException:地址族不受協議支持]注意:有一件事,「應用程序mibooks意外停止」錯誤只有當我給這個聲明「intent.setType(」* zip *「);」如果我評論一個工作正常,但文件不會被下載。 – vnshetty 2011-02-24 05:00:00

回答

1

Android應用程序意外停止它採用的是Android編程正常情況時發生的任何運行時錯誤。當我拋出這個問題時,我並沒有意識到這一點。我得到了這個問題的解決方案,即.. 1. setContentView(R.layout.webview);和setContentView(R.layout.main);一個佈局指向null。