我用下面的代码来下载文件。它运行良好,但是当我单击下载按钮时,出现以下错误:“应用程序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);
}
});
}
});
}
}
请提供堆栈跟踪。 – Mudassir 2011-02-24 04:21:28
我希望你在使用Eclipse。切换到DDMS的角度,然后点击Logcat,在这里你会看到所有的调试,警告和错误日志。每当Android抛出任何异常时,它都会在DDMS中生成错误日志,告诉我们这个问题。 – Mudassir 2011-02-24 04:32:21
是的,你是对的..它显示了许多行我无法弄清楚。最后一行是:[02-24 10:21:54.381:DEBUG/SntpClient(67):请求时间失败:java.net.SocketException:地址族不受协议支持]注意:有一件事,“应用程序mibooks意外停止”错误只有当我给这个声明“intent.setType(”* zip *“);”如果我评论一个工作正常,但文件不会被下载。 – vnshetty 2011-02-24 05:00:00