Externalization/Serialization Not Working?
I am trying to pass a checklist object over an intent to the next activity. Here's the code:
CheckList Object (CheckList.java)
package com.test.serialization;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import android.content.Context;
import android.widget.TableLayout;
public class CheckList extends TableLayout implements Externalizable {
public String name;
public int number_of_rows;
public CheckList() {
super(SerializationActivity.context);
}
public CheckList(Context context) {
super(context);
}
@Override
public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException {
// TODO Auto-generated method stub
}
@Override
public void writeExternal(ObjectOutput output) throws IOException {
// TODO Auto-generated method stub
}
}
Serialization Activity (SerializationActivity.java)
package com.test.serialization;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SerializationActivity extends Activity {
/** Called when the activity is first created. */
private SerializationActivity activity;
private CheckList checklist;
public static Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this.getApplicationContext();
checklist = new CheckList(this.ge开发者_开发知识库tApplicationContext());
checklist.name="asdasd";
checklist.number_of_rows= 5;
activity = this;
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(on_click_listener);
}
private OnClickListener on_click_listener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(activity, DeserializationActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("checklist", checklist);
intent.putExtra("checklist_bundle", bundle);
startActivity(intent);
}
};
}
package com.test.serialization;
import android.os.Bundle;
import android.util.Log;
public class DeserializationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
try {
Bundle bundle = this.getIntent().getExtras();
Bundle checklist_bundle = bundle.getBundle("checklist_bundle");
CheckList checklist = (CheckList) checklist_bundle.getSerializable("checklist");
Log.d("LOG_TAG", checklist.name);
Log.d("LOG_TAG", checklist.number_of_rows);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I know it's weird to call the the context from the Activity through the static way, but I can't figure how can i do so without it when trying to serialize a view object.
The problem here is that my checklist name and number of rows will be empty and 0.
how can i pass in the values correctly?
In order to serialize an object you need to serialize the whole object graph, i.e. the top object and all objects it refers to and so on. If you try to serialize system provided objects you might encounter some native (OS provided) objects which can't be serialized. That's why serializing system objects is mostly impossible.
For the purpose of sending Views around the Android OS (e.g. Via Intent), there is a special class that you should use: RemoteViews
RemoteViews
is not a view, but rather a series of commands how to build a View. You can create it from XML layout resource, manipulate it. Since it implements Parcelable you can easily add it to Intent via intent.putExtra(name, remoteViews)
.
you are not doing anything in writeExternal method...
its better (in your case) to use serializable interface instead of externalizable ..
Externalizable is an interface which gives the programmer the option to externalize the object in consideration (since you didn't write anything to the stream its not passing an object across).
instead of Externalizable if you use Serializable things would get serialized automatically & things would start to work.
精彩评论