当我尝试通过单击界面上的按钮从我的应用程序中打开 Web 浏览器时导致崩溃的代码下方
MainTest.java
package com.heeere.androiddnssd.discovery;
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.widget.Button;
public class MainTest extends Activity {
private Discovery discovery;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ( savedInstanceState == null)
discovery = new Discovery(this);
else
discovery = (Discovery) savedInstanceState.getSerializable("discovery");
setContentView(R.layout.main);
Button b = (Button)this.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
});
}
@Override
protected void onSaveInstanceState(final Bundle outState) {
outState.putSerializable("discovery", discovery);
super.onSaveInstanceState(outState);
}
}
发现.java
package com.heeere.androiddnssd.discovery;
import java.io.Serializable;
public class Discovery implements Serializable {
private static final long serialVersionUID = 637576886455091135L;
private MainTest maintest;
//android.os.Handler handler = new android.os.Handler();
public Discovery (MainTest maintest) {
this.maintest = maintest;
}
}
现在,如果我从 Discovery 的构造函数中删除 MainTest Class 类型,该错误将消失。所以这就是坠机的原因。
如何在不从 Discovery 的构造函数中删除 MainTest 类类型的情况下解决此问题?