当从我的应用程序加载网页broser序列化的崩溃错误 [英] Serializable crash error when loading web broser from my application
问题描述
我开发的应用程序。
该应用程序包含2班
-
它实现序列化的探索类
-
它扩展了活动的主要类
在主类我呼吁在应用程序界面中的按钮,点击Android的网络浏览器。而这种行为产生碰撞。
波纹管是code:
MainTest.java
包com.heeere.androiddnssd.discovery;
进口android.annotation.Sup pressLint;
进口android.app.Activity;
进口android.content.Intent;
进口android.net.Uri;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.TextView;
公共类MainTest延伸活动{
android.net.wifi.WifiManager.MulticastLock锁;
私人Discovery探索;
私人的TextView TextView的;
/ **第一次创建活动时调用。 * /
@燮pressLint(NewApi)@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
如果(savedInstanceState == NULL)
发现=新发现(本);
其他
发现=(发现)savedInstanceState.getSerializable(发现);
的setContentView(R.layout.main);
TextView的=(的TextView)this.findViewById(R.id.text);
按钮B =(按钮)this.findViewById(R.id.button);
b.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
//discovery.setUp();
意图browserIntent =新的意图(Intent.ACTION_VIEW,Uri.parse(http://www.google.com));
startActivity(browserIntent);
}
});
android.net.wifi.WifiManager无线=(android.net.wifi.WifiManager)getSystemService(android.content.Context.WIFI_SERVICE);
锁定= wifi.createMulticastLock(mylockthereturn);
lock.setReferenceCounted(真正的);
lock.acquire();
}
@覆盖
保护无效的onSaveInstanceState(最终捆绑outState){
outState.putSerializable(发现,发现);
super.onSaveInstanceState(outState);
}
公共无效的UpdateView(){
弦乐味精= discovery.getMsg();
textView.setText(MSG);
}
@燮pressLint(NewApi)@覆盖
保护无效的onStop(){
discovery.stop();
lock.release();
super.onStop();
}
}
Discovery.java
包com.heeere.androiddnssd.discovery;
进口java.io.IOException异常;
的Bean;
进口javax.jmdns.JmDNS;
进口javax.jmdns.ServiceEvent;
进口javax.jmdns.ServiceListener;
公共类探索实现Serializable {
私有静态最后长的serialVersionUID = 637576886455091135L;
私人字符串类型=_ikunet._tcp.local。
私人弦乐味精=;
私人JmDNS jmdns = NULL;
私人的ServiceListener监听= NULL;
私人MainTest maintest;
android.os.Handler处理器=新android.os.Handler();
市民发现(MainTest maintest){
this.maintest = maintest;
建立();
}
公共无效设置(){
尝试 {
jmdns = JmDNS.create();
jmdns.addServiceListener(类型,听者=新的ServiceListener(){
公共无效serviceResolved(ServiceEvent EV){
。MSG = MSG + ev.getInfo()的getName()+\ N的;
更新();
}
公共无效serviceRemoved(ServiceEvent EV){
}
公共无效serviceAdded(ServiceEvent事件){
jmdns.requestServiceInfo(event.getType(),event.getName(),1);
}
});
}赶上(IOException异常E){
//e.printStackTrace();
返回;
}
}
公众的getMsg字符串(){
返回味精;
}
私人无效更新(){
handler.postDelayed(新的Runnable(){
公共无效的run(){
maintest.updateView();
}
},1);
}
公共无效停止(){
如果(jmdns!= NULL){
如果(听众!= NULL){
jmdns.removeServiceListener(类型,侦听);
监听= NULL;
}
jmdns.unregisterAllServices();
尝试 {
jmdns.close();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
jmdns = NULL;
}
}
}
LogCat中
09-13 15:08:30.507:E / AndroidRuntime(15775):未捕获的处理程序:螺纹主力退出,由于未捕获的异常
09-13 15:08:30.517:E / AndroidRuntime(15775):java.lang.RuntimeException的:Parcelable遇到IOException异常写序列化对象(名称= com.heeere.androiddnssd.discovery.Discovery)
09-13 15:08:30.517:E / AndroidRuntime(15775):在android.os.Parcel.writeSerializable(Parcel.java:1131)
09-13 15:08:30.517:E / AndroidRuntime(15775):在android.os.Parcel.writeValue(Parcel.java:1085)
09-13 15:08:30.517:E / AndroidRuntime(15775):在android.os.Parcel.writeMapInternal(Parcel.java:469)
09-13 15:08:30.517:E / AndroidRuntime(15775):在android.os.Bundle.writeToParcel(Bundle.java:1445)
09-13 15:08:30.517:E / AndroidRuntime(15775):在android.os.Parcel.writeBundle(Parcel.java:483)
09-13 15:08:30.517:E / AndroidRuntime(15775):在android.app.ActivityManagerProxy.activityPaused(ActivityManagerNative.java:1427)
09-13 15:08:30.517:E / AndroidRuntime(15775):在android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3106)
09-13 15:08:30.517:E / AndroidRuntime(15775):在android.app.ActivityThread.access $ 2400(ActivityThread.java:119)
09-13 15:08:30.517:E / AndroidRuntime(15775):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1870)
09-13 15:08:30.517:E / AndroidRuntime(15775):在android.os.Handler.dispatchMessage(Handler.java:99)
09-13 15:08:30.517:E / AndroidRuntime(15775):在android.os.Looper.loop(Looper.java:123)
09-13 15:08:30.517:E / AndroidRuntime(15775):在android.app.ActivityThread.main(ActivityThread.java:4363)
09-13 15:08:30.517:E / AndroidRuntime(15775):在java.lang.reflect.Method.invokeNative(本机方法)
09-13 15:08:30.517:E / AndroidRuntime(15775):在java.lang.reflect.Method.invoke(Method.java:521)
09-13 15:08:30.517:E / AndroidRuntime(15775):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
09-13 15:08:30.517:E / AndroidRuntime(15775):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-13 15:08:30.517:E / AndroidRuntime(15775):在dalvik.system.NativeStart.main(本机方法)
09-13 15:08:30.517:E / AndroidRuntime(15775):java.io.NotSerializableException:产生的原因android.os.Handler
09-13 15:08:30.517:E / AndroidRuntime(15775):在java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1547)
09-13 15:08:30.517:E / AndroidRuntime(15775):在java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1854)
09-13 15:08:30.517:E / AndroidRuntime(15775):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1696)
09-13 15:08:30.517:E / AndroidRuntime(15775):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1660)
09-13 15:08:30.517:E / AndroidRuntime(15775):在java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1153)
09-13 15:08:30.517:E / AndroidRuntime(15775):在java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:420)
09-13 15:08:30.517:E / AndroidRuntime(15775):在java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1251)
09-13 15:08:30.517:E / AndroidRuntime(15775):在java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1587)
09-13 15:08:30.517:E / AndroidRuntime(15775):在java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1854)
09-13 15:08:30.517:E / AndroidRuntime(15775):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1696)
09-13 15:08:30.517:E / AndroidRuntime(15775):在java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1660)
09-13 15:08:30.517:E / AndroidRuntime(15775):在android.os.Parcel.writeSerializable(Parcel.java:1126)
09-13 15:08:30.517:E / AndroidRuntime(15775):16 ...更多
这个问题是由于创建其相关对象时,主要活动传递给序列化类。在我们的情况下通过了 MainTest
反对搜索
创建它时的对象。
解决方案:创建一个包含所有发现数据的另一个序列化类(数据是serialzable)
I'm developing an application.
This application contains 2 classes
Discovery class which implements serializable
Main class which extends Activity
In the Main class I call the android web browser on a click on the button in the application interface . And this behaviour generate a crash.
bellow is the code:
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();
}
}
Discovery.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;
}
}
}
LogCat
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
the problem is due to pass main activity to serializable class when creating its related object. in our case passing the MainTest
Object to the Discovery
object when creating it.
The solution: create another serializable class which contains all Discovery data (data to be serialzable).
这篇关于当从我的应用程序加载网页broser序列化的崩溃错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!