我一直在尝试将一个类的实例从一个Activity传递给另一个Activity。
到目前为止,我只获得java.io.NotSerializableException
例外。
首先,这些链接到我设置为扩展Serializable的类,当这些错误停止时,它们开始链接到Activity,我通过Intent和putExtra发送实例。
以下是导致问题的代码:
Intent i = new Intent(NewGame.this, GameActivity.class);
i.putExtra("existing", true);
i.putExtra("customClass", gh);<--- THIS is the line for the Serializable class I send
startActivity(i);<--- Here I get NotSerializableException
NotSerializableException首先链接到NewGame(Intent所在的活动),然后在我尝试扩展serializable后,它开始在Button和RadioGroup(View-extends)等字段上显示错误。
如果我让NewGame没有实现Serializable,我会回到NewGame作为意图从发送问题的活动。
所以:
i.putExtra(String name, Serializable s)
是否需要发送Intent的Activity才能实现Serializable?如果不是,为什么startActivity会对我发送意图的活动给我一个例外?
更多代码:
public class NewGame extends Activity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener,
RadioGroup.OnCheckedChangeListener, AdapterView.OnItemSelectedListener, Serializable{
SeekBar sb;
TextView tvAI;
Button start, custAI;<--- This gives an error for some reason
int max = 9;
int min = 1;
int step = 1;
RadioGroup uniSize, aiDiff;<--- This gives an error for some reason
Spinner spinner;<-- This gives an error for some reason
int diff = 0;
public void onCreate(Bundle sis){
super.onCreate(sis);
setContentView(R.layout.startgame);
initialization of etc views
}
//Irrelevant code between create and onCreate is removed
private void create(){
setContentView(R.layout.loading);
Thread thread = new Thread() {
@Override
public void run() {
try {
super.run();
sleep(500); //initial tiny delay
//GameHandler implements serializable
GameHandler gh = new GameHandler(stars, NewGame.this, AIs);//The GameHandler constructor takes in: int, Context, int
(etc other code)
gh.initGen(ais, player);
gh.save();
IntentHandler i = new IntentHandler();
Intent i = new Intent(instance, GameActivity.class);
i.putExtra("existingGH", true);
i.putExtra("gameHandler", gh);<--- This is where the app would stop if the issue is related to fields in GameHandler(gh)
instance.startActivity(i);<--- THIS is where the app stops working. It shows an exception related to the fields of NewGame(the class the intent is sent from and this code is in)
Log.e("DEBUG", "Leaving Activity NewGame");
finish();
join();
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.start();
}
}
GameHandler:
public class GameHandler implements Serializable{
int stars;
int AIEntities;
private Context c;
private int width, height;
private Random r;
private Game game;
private Neutral n;
/**
* Load the game.
* @param c
*/
public GameHandler(Context c, Game game){
this.game = game;
this.c = c;
(etc irrelevant code)
}
public GameHandler(int stars, Context c, int AIEntities){
this.c = c;
this.stars = stars;
this.AIEntities = AIEntities;
(etc initialization)
ais = new ArrayList<>();
}
public void initGen(ArrayList<Entity> ais, Player player){
this.ais = ais;
this.player = player;
generate();
//
save();
}
public void save(){
//saving
}
Rect[] rect;
private int sx, sy, sclas, smass;
ArrayList<Stars> solar;
public void generate() {
...
}
public ArrayList<Stars> getStars(){
return solar;
}
public void render(Canvas c){
//rendering
}
//Needs to be transient to avoid error(Entity causes error)
public transient ArrayList<Entity> ais;
(other code, not relevant)
}
堆栈跟踪:
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.mypackage.game.GameHandler)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at android.os.Parcel.writeSerializable(Parcel.java:1468)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at android.os.Parcel.writeValue(Parcel.java:1416)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at android.os.Bundle.writeToParcel(Bundle.java:1079)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at android.os.Parcel.writeBundle(Parcel.java:711)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at android.content.Intent.writeToParcel(Intent.java:8778)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3112)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1540)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at android.app.Activity.startActivityForResult(Activity.java:4283)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at android.app.Activity.startActivityForResult(Activity.java:4230)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at android.app.Activity.startActivity(Activity.java:4567)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at android.app.Activity.startActivity(Activity.java:4535)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at com.mypackage.NewGame$3.run(NewGame.java:320)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: Caused by: java.io.NotSerializableException: com.mypackage.NewGame
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: at android.os.Parcel.writeSerializable(Parcel.java:1463)
03-25 18:42:46.371 1423-2474/com.mypackage W/System.err: ... 13 more
答案 0 :(得分:2)
i.putExtra(String name,Serializable s)是否需要发送Intent的Activity才能实现Serializable?
不,但确实意味着s
无法引用您需要采取的活动或其他非Serializable
内容或防止序列化尝试序列化它们的步骤(例如,覆盖所描述的方法in the documentation)。
它在NewGame类中的对象上触发,该类在gh
中没有实例
是的,确实如此。就在这里:
private Context c;
首先,Context
不是Serializable
。其次,Activity
继承自Context
,c
是您的NewGame
活动,因为您将其分配给c
。