我正在开发一个应用程序。
此应用程序包含 2 个类
实现可序列化的发现类
扩展 Activity 的主类
在 Main 类中,我通过单击应用程序界面中的按钮调用 android Web 浏览器。这种行为会导致崩溃。
下面是代码:
MainTest.java
package com.heeere.androiddnssd.discovery;
import android.annotation.SuppressLint;
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;
import android.widget.TextView;
public class MainTest extends Activity {
android.net.wifi.WifiManager.MulticastLock lock;
private Discovery discovery;
private TextView textView;
/** Called when the activity is first created. */
@SuppressLint("NewApi") @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);
textView = (TextView)this.findViewById(R.id.text);
Button b = (Button)this.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//discovery.setUp();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
});
android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) getSystemService(android.content.Context.WIFI_SERVICE);
lock = wifi.createMulticastLock("mylockthereturn");
lock.setReferenceCounted(true);
lock.acquire();
}
@Override
protected void onSaveInstanceState(final Bundle outState) {
outState.putSerializable("discovery", discovery);
super.onSaveInstanceState(outState);
}
public void updateView () {
String msg = discovery.getMsg();
textView.setText(msg);
}
@SuppressLint("NewApi") @Override
protected void onStop() {
discovery.stop();
lock.release();
super.onStop();
}
}
发现.java
package com.heeere.androiddnssd.discovery;
import java.io.IOException;
import java.io.Serializable;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceListener;
public class Discovery implements Serializable {
private static final long serialVersionUID = 637576886455091135L;
private String type = "_ikunet._tcp.local.";
private String msg="";
private JmDNS jmdns = null;
private ServiceListener listener = null;
private MainTest maintest;
android.os.Handler handler = new android.os.Handler();
public Discovery (MainTest maintest) {
this.maintest = maintest;
setUp();
}
public void setUp() {
try {
jmdns = JmDNS.create();
jmdns.addServiceListener(type, listener = new ServiceListener() {
public void serviceResolved(ServiceEvent ev) {
msg = msg + ev.getInfo().getName()+ "\n";
update();
}
public void serviceRemoved(ServiceEvent ev) {
}
public void serviceAdded(ServiceEvent event) {
jmdns.requestServiceInfo(event.getType(), event.getName(), 1);
}
});
} catch (IOException e) {
//e.printStackTrace();
return;
}
}
public String getMsg() {
return msg;
}
private void update() {
handler.postDelayed(new Runnable() {
public void run() {
maintest.updateView();
}
}, 1);
}
public void stop() {
if (jmdns != null) {
if (listener != null) {
jmdns.removeServiceListener(type, listener);
listener = null;
}
jmdns.unregisterAllServices();
try {
jmdns.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jmdns = null;
}
}
}
日志猫
09-13 15:08:30.507: E/AndroidRuntime(15775): Uncaught handler: thread main exiting due to uncaught exception
09-13 15:08:30.517: E/AndroidRuntime(15775): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.heeere.androiddnssd.discovery.Discovery)
09-13 15:08:30.517: E/AndroidRuntime(15775): at android.os.Parcel.writeSerializable(Parcel.java:1131)
09-13 15:08:30.517: E/AndroidRuntime(15775): at android.os.Parcel.writeValue(Parcel.java:1085)
09-13 15:08:30.517: E/AndroidRuntime(15775): at android.os.Parcel.writeMapInternal(Parcel.java:469)
09-13 15:08:30.517: E/AndroidRuntime(15775): at android.os.Bundle.writeToParcel(Bundle.java:1445)
09-13 15:08:30.517: E/AndroidRuntime(15775): at android.os.Parcel.writeBundle(Parcel.java:483)
09-13 15:08:30.517: E/AndroidRuntime(15775): at android.app.ActivityManagerProxy.activityPaused(ActivityManagerNative.java:1427)
09-13 15:08:30.517: E/AndroidRuntime(15775): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3106)
09-13 15:08:30.517: E/AndroidRuntime(15775): at android.app.ActivityThread.access$2400(ActivityThread.java:119)
09-13 15:08:30.517: E/AndroidRuntime(15775): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1870)
09-13 15:08:30.517: E/AndroidRuntime(15775): at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 15:08:30.517: E/AndroidRuntime(15775): at android.os.Looper.loop(Looper.java:123)
09-13 15:08:30.517: E/AndroidRuntime(15775): at android.app.ActivityThread.main(ActivityThread.java:4363)
09-13 15:08:30.517: E/AndroidRuntime(15775): at java.lang.reflect.Method.invokeNative(Native Method)
09-13 15:08:30.517: E/AndroidRuntime(15775): at java.lang.reflect.Method.invoke(Method.java:521)
09-13 15:08:30.517: E/AndroidRuntime(15775): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-13 15:08:30.517: E/AndroidRuntime(15775): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-13 15:08:30.517: E/AndroidRuntime(15775): at dalvik.system.NativeStart.main(Native Method)
09-13 15:08:30.517: E/AndroidRuntime(15775): Caused by: java.io.NotSerializableException: android.os.Handler
09-13 15:08:30.517: E/AndroidRuntime(15775): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1547)
09-13 15:08:30.517: E/AndroidRuntime(15775): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1854)
09-13 15:08:30.517: E/AndroidRuntime(15775): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1696)
09-13 15:08:30.517: E/AndroidRuntime(15775): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1660)
09-13 15:08:30.517: E/AndroidRuntime(15775): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
09-13 15:08:30.517: E/AndroidRuntime(15775): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
09-13 15:08:30.517: E/AndroidRuntime(15775): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)
09-13 15:08:30.517: E/AndroidRuntime(15775): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
09-13 15:08:30.517: E/AndroidRuntime(15775): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1854)
09-13 15:08:30.517: E/AndroidRuntime(15775): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1696)
09-13 15:08:30.517: E/AndroidRuntime(15775): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1660)
09-13 15:08:30.517: E/AndroidRuntime(15775): at android.os.Parcel.writeSerializable(Parcel.java:1126)
09-13 15:08:30.517: E/AndroidRuntime(15775): ... 16 more