我使用以下代码下载文件。它运行正常,但是当我单击下载按钮时,出现以下错误:“应用程序mibooks意外停止。”
如何在我的代码中解决此问题?
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);
}
});
}
});
}
}
答案 0 :(得分:1)
Android应用程序意外停止当发生任何运行时错误时,这是android编程中的正常情况。当我抛出这个问题时,我并没有意识到这一点。我得到了这个问题的解决方案.. 1. setContentView(R.layout.webview);和setContentView(R.layout.main);一个布局指向null。
答案 1 :(得分:1)
也许安装ADT插件?见这里: - Cannot Make Andoid Apps Work