我想将ArrayList<Links>
的一个对象传递给Next Activity。
我已检查过使用Parcelable
来实现此目的但我在Stream
类中有Links
类的另一个类对象。
如何将此链接对象传递给下一个活动
Links
class:public class Links implements Parcelable {
private String name;
private Streams streams;
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param streams the streams to set
*/
public void setStreams(Streams streams) {
this.streams = streams;
}
/**
* @return the streams
*/
public Streams getStreams() {
return streams;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
/**
** WHAT CAN I DO HERE...???
**/
}
}
Stream
分类:public class Streams {
private String link;
private String stream;
/**
* @param link the link to set
*/
public void setLink(String link) {
this.link = link;
}
/**
* @return the link
*/
public String getLink() {
return link;
}
/**
* @param stream the stream to set
*/
public void setStream(String stream) {
this.stream = stream;
}
/**
* @return the stream
*/
public String getStream() {
return stream;
}
}
我想将ArrayList对象发送到下一个活动。我怎么能够?
我在Logcat:
06-19 16:23:00.820: E/AndroidRuntime(21080): FATAL EXCEPTION: main
06-19 16:23:00.820: E/AndroidRuntime(21080): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gtu.gtupapers/com.gtu.gtupapers.StreamSelection}: java.lang.NullPointerException: expected receiver of type com.gtu.gtupapers.Links, but got null
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2129)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.app.ActivityThread.access$700(ActivityThread.java:146)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.os.Looper.loop(Looper.java:137)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.app.ActivityThread.main(ActivityThread.java:4949)
06-19 16:23:00.820: E/AndroidRuntime(21080): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 16:23:00.820: E/AndroidRuntime(21080): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 16:23:00.820: E/AndroidRuntime(21080): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
06-19 16:23:00.820: E/AndroidRuntime(21080): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
06-19 16:23:00.820: E/AndroidRuntime(21080): at dalvik.system.NativeStart.main(Native Method)
06-19 16:23:00.820: E/AndroidRuntime(21080): Caused by: java.lang.NullPointerException: expected receiver of type com.gtu.gtupapers.Links, but got null
06-19 16:23:00.820: E/AndroidRuntime(21080): at java.lang.reflect.Field.getField(Native Method)
06-19 16:23:00.820: E/AndroidRuntime(21080): at java.lang.reflect.Field.get(Field.java:269)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.os.Parcel.readParcelable(Parcel.java:2066)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.os.Parcel.readValue(Parcel.java:1965)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.os.Parcel.readListInternal(Parcel.java:2235)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.os.Parcel.readArrayList(Parcel.java:1655)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.os.Parcel.readValue(Parcel.java:1986)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.os.Parcel.readMapInternal(Parcel.java:2226)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.os.Bundle.unparcel(Bundle.java:223)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.os.Bundle.getParcelableArrayList(Bundle.java:1209)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.content.Intent.getParcelableArrayListExtra(Intent.java:4477)
06-19 16:23:00.820: E/AndroidRuntime(21080): at com.gtu.gtupapers.StreamSelection.onCreate(StreamSelection.java:16)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.app.Activity.performCreate(Activity.java:5185)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-19 16:23:00.820: E/AndroidRuntime(21080): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
06-19 16:23:00.820: E/AndroidRuntime(21080): ... 11 more
您的帮助将不胜感激......
答案 0 :(得分:2)
Links
上课:
public class Links implements Parcelable {
private String name;
private Streams streams;
public Links(String name, Streams streams) {
this.name = name;
this.streams = streams;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param streams the streams to set
*/
public void setStreams(Streams streams) {
this.streams = streams;
}
/**
* @return the streams
*/
public Streams getStreams() {
return streams;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeParcelable(streams, flags);
}
public static Parcelable.Creator<Links> CREATOR = new Parcelable.Creator<Links>() {
@Override
public Links createFromParcel(Parcel source) {
String name = source.readString();
Streams streams = source.readParcelable(Streams.class.getClassLoader());
return new Links(name, streams);
}
@Override
public Links[] newArray(int size) {
return new Links[size];
}
};
}
Streams
上课:
public class Streams implements Parcelable {
private String link;
private String stream;
public Streams(String link, String stream) {
this.link = link;
this.stream = stream;
}
/**
* @param link the link to set
*/
public void setLink(String link) {
this.link = link;
}
/**
* @return the link
*/
public String getLink() {
return link;
}
/**
* @param stream the stream to set
*/
public void setStream(String stream) {
this.stream = stream;
}
/**
* @return the stream
*/
public String getStream() {
return stream;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(link);
dest.writeString(stream);
}
public static final Parcelable.Creator<Streams> CREATOR = new Parcelable.Creator<Streams>() {
@Override
public Streams createFromParcel(Parcel source) {
String link = source.readString();
String stream = source.readString();
return new Streams(link, stream);
}
@Override
public Streams[] newArray(int size) {
return new Streams[size];
}
};
}
然后你可以传递清单:
Intent intent = new Intent(this, OtherActivity.class);
intent.putParcelableArrayListExtra("links", yourList);
startActivity(intent);
在OtherActivity
:
Intent intent = getIntent();
ArrayList<Links> links = intent.getParcelableArrayListExtra("links");
答案 1 :(得分:2)
答案 2 :(得分:1)
感谢WonderCsabo创建了类Parcelable,但我使用此link创建了所以感谢Zhuinden this post。
我发布了完整的答案,对其他人也有帮助。
public class Links implements Parcelable {
private String name;
private Streams streams;
public Links() {
// TODO Auto-generated constructor stud
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param streams the streams to set
*/
public void setStreams(Streams streams) {
this.streams = streams;
}
/**
* @return the streams
*/
public Streams getStreams() {
return streams;
}
protected Links(Parcel in) {
name = in.readString();
streams = (Streams) in.readValue(Streams.class.getClassLoader());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeValue(streams);
}
public static final Parcelable.Creator<Links> CREATOR = new Parcelable.Creator<Links>() {
@Override
public Links createFromParcel(Parcel in) {
return new Links(in);
}
@Override
public Links[] newArray(int size) {
return new Links[size];
}
};
}
public class Streams implements Parcelable {
private String link;
private String stream;
Streams() {
// TODO Auto-generated constructor stub
}
protected Streams(Parcel in) {
link = in.readString();
stream = in.readString();
}
/**
* @param link the link to set
*/
public void setLink(String link) {
this.link = link;
}
/**
* @return the link
*/
public String getLink() {
return link;
}
/**
* @param stream the stream to set
*/
public void setStream(String stream) {
this.stream = stream;
}
/**
* @return the stream
*/
public String getStream() {
return stream;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(link);
dest.writeString(stream);
}
public static final Parcelable.Creator<Streams> CREATOR = new Parcelable.Creator<Streams>() {
@Override
public Streams createFromParcel(Parcel in) {
return new Streams(in);
}
@Override
public Streams[] newArray(int size) {
return new Streams[size];
}
};
}
然后最后使用:
发送到下一个Activity
Intent intent = new Intent(MainActivity.this, StreamSelection.class);
intent.putExtra("position", position);
intent.putParcelableArrayListExtra("list", arrayList);
startActivity(intent);
接收:
ArrayList<Links> links = getIntent().getParcelableArrayListExtra("list");
int pos = getIntent().getExtras().getInt("position");
Log.d("TAG", "Links: " + links.get(pos).getName());
感谢您的帮助。
答案 3 :(得分:0)
在 Links
类中实施Serializable。
public class Links implements Serializable {
private String name;
private Streams streams;
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param streams the streams to set
*/
public void setStreams(Streams streams) {
this.streams = streams;
}
/**
* @return the streams
*/
public Streams getStreams() {
return streams;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
}
我们将对象A发送给B
Intent i = new Intent(this, B.class);
i.putExtra("Object", **Link class object**);
startActivity(i);
和B活动
Intent i = getIntent();
Links link = (Links)i.getSerializableExtra("Object");