Intent putExtra ArrayList <namevaluepair> </namevaluepair>

时间:2013-08-05 03:04:25

标签: android android-intent

有没有人知道如何将ArrayList<NameValuePair>添加到Intent作为额外的?

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("first_name", first_name));
nameValuePairs.add(new BasicNameValuePair("last_name", last_name));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("password", password));

/* Move on to step 2 */
Intent intent = new Intent(RegisterActivity1.this, RegisterActivity2.class);
intent.putExtra("nvp", nameValuePairs);
startActivity(intent);

这是RegisterActivity2的类声明:

public class RegisterActivity2 extends Activity implements Serializable {
}

logcat中的错误是:

> Parcel: unable to marshal value first_name="whatever"

在实施Ted Hopp的建议后,我仍然收到错误。这是堆栈跟踪:

  

08-04 22:10:16.095: E/AndroidRuntime(5065): FATAL EXCEPTION: main
> 08-04 22:10:16.095: E/AndroidRuntime(5065):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{/.RegisterActivity2}: java.lang.RuntimeException:
> Parcelable encountered IOException reading a Serializable object (name
> = database.NVP) 08-04 22:10:16.095: E/AndroidRuntime(5065):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
> 08-04 22:10:16.095: E/AndroidRuntime(5065):   at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
> 08-04 22:10:16.095: E/AndroidRuntime(5065):   at
> android.app.ActivityThread.access$600(ActivityThread.java:135) 08-04
> 22:10:16.095: E/AndroidRuntime(5065):     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
> 08-04 22:10:16.095: E/AndroidRuntime(5065):   at
> android.os.Handler.dispatchMessage(Handler.java:99) 08-04
> 22:10:16.095: E/AndroidRuntime(5065):     at
> android.os.Looper.loop(Looper.java:137) 08-04 22:10:16.095:
> E/AndroidRuntime(5065):   at
> android.app.ActivityThread.main(ActivityThread.java:4849) 08-04
> 22:10:16.095: E/AndroidRuntime(5065):     at
> java.lang.reflect.Method.invokeNative(Native Method) 08-04
> 22:10:16.095: E/AndroidRuntime(5065):     at
> java.lang.reflect.Method.invoke(Method.java:511) 08-04 22:10:16.095:
> E/AndroidRuntime(5065):   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
> 08-04 22:10:16.095: E/AndroidRuntime(5065):   at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 08-04
> 22:10:16.095: E/AndroidRuntime(5065):     at
> dalvik.system.NativeStart.main(Native Method) 08-04 22:10:16.095:
> E/AndroidRuntime(5065): Caused by: java.lang.RuntimeException:
> Parcelable encountered IOException reading a Serializable object (name
> = database.NVP) 08-04 22:10:16.095: E/AndroidRuntime(5065):   at android.os.Parcel.readSerializable(Parcel.java:2144) 08-04
> 22:10:16.095: E/AndroidRuntime(5065):     at
> android.os.Parcel.readValue(Parcel.java:2016) 08-04 22:10:16.095:
> E/AndroidRuntime(5065):   at
> android.os.Parcel.readListInternal(Parcel.java:2235) 08-04
> 22:10:16.095: E/AndroidRuntime(5065):     at
> android.os.Parcel.readArrayList(Parcel.java:1655) 08-04 22:10:16.095:
> E/AndroidRuntime(5065):   at
> android.os.Parcel.readValue(Parcel.java:1986) 08-04 22:10:16.095:
> E/AndroidRuntime(5065):   at
> android.os.Parcel.readMapInternal(Parcel.java:2226) 08-04
> 22:10:16.095: E/AndroidRuntime(5065):     at
> android.os.Bundle.unparcel(Bundle.java:223) 08-04 22:10:16.095:
> E/AndroidRuntime(5065):   at
> android.os.Bundle.getSerializable(Bundle.java:1254) 08-04
> 22:10:16.095: E/AndroidRuntime(5065):     at
> android.content.Intent.getSerializableExtra(Intent.java:4268) 08-04
> 22:10:16.095: E/AndroidRuntime(5065):     at
> .RegisterActivity2.onCreate(RegisterActivity2.java:24) 08-04
> 22:10:16.095: E/AndroidRuntime(5065):     at
> android.app.Activity.performCreate(Activity.java:5244) 08-04
> 22:10:16.095: E/AndroidRuntime(5065):     at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
> 08-04 22:10:16.095: E/AndroidRuntime(5065):   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
> 08-04 22:10:16.095: E/AndroidRuntime(5065):   ... 11 more 08-04
> 22:10:16.095: E/AndroidRuntime(5065): Caused by:
> java.io.InvalidClassException:
> org.apache.http.message.BasicNameValuePair; IllegalAccessException
> 08-04 22:10:16.095: E/AndroidRuntime(5065):   at
> java.io.ObjectStreamClass.resolveConstructorClass(ObjectStreamClass.java:694)
> 08-04 22:10:16.095: E/AndroidRuntime(5065):   at
> java.io.ObjectStreamClass.newInstance(ObjectStreamClass.java:655)
> 08-04 22:10:16.095: E/AndroidRuntime(5065):   at
> java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1816)
> 08-04 22:10:16.095: E/AndroidRuntime(5065):   at
> java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
> 08-04 22:10:16.095: E/AndroidRuntime(5065):   at
> java.io.ObjectInputStream.readObject(ObjectInputStream.java:2003)
> 08-04 22:10:16.095: E/AndroidRuntime(5065):   at
> java.io.ObjectInputStream.readObject(ObjectInputStream.java:1960)
> 08-04 22:10:16.095: E/AndroidRuntime(5065):   at
> android.os.Parcel.readSerializable(Parcel.java:2142) 08-04
> 22:10:16.095: E/AndroidRuntime(5065):     ... 23 more

5 个答案:

答案 0 :(得分:24)

正如其他人所指出的那样,当您想要将数组作为intent extra传递时,元素需要实现Serializable。在你的情况下,方法是相当简单(不完全 - 见下文):定义你自己的BasicNameValuePair子类并声明它来实现接口。

public class NVP extends BasicNameValuePair implements Serializable {
    public NVP(String name, String value) {
        super(name, value);
    }
}

然后就像你一直在尝试一样使用它:

ArrayList<NVP> nameValuePairs = new ArrayList<NVP>();
nameValuePairs.add(new NVP("first_name", first_name));
nameValuePairs.add(new NVP("last_name", last_name));
nameValuePairs.add(new NVP("email", email));
nameValuePairs.add(new NVP("password", password));

/* Move on to step 2 */
Intent intent = new Intent(RegisterActivity1.this, RegisterActivity2.class);
intent.putExtra("nvp", nameValuePairs);
startActivity(intent);

在接收端,您需要将其拉出:

ArrayList<NVP> nameValuePairs = (ArrayList<NVP>) intent.getSerializable("nvp");

编辑好吧,上面的代码不起作用,因为BasicNameValuePair没有默认构造函数,序列化机制要求不可序列化的祖先类具有默认构造函数。否则,您将在反序列化时获得异常。

不幸的是,这意味着解决方案并不像我说的那么简单。一种解决方法是定义您自己的类来实现Parcelable@keerthana murugesan suggests in his answer。如果您之前已经实现了Parcelable,那么您知道这有点痛苦。另一个解决方法是定义一个包装BasicNameValuePair(而不是从中派生)并管理自己的序列化的类:

public class NVP implements NameValuePair, Serializable {
    private BasicNameValuePair nvp;

    public NVP(String name, String value) {
        nvp = new BasicNameValuePair(name, value);
    }

    @Override
    public String getName() {
        return nvp.getName();
    }

    @Override
    public String getValue() {
        return nvp.getValue();
    }

    // serialization support

    private static final long serialVersionUID = 1L;

    private void writeObject(ObjectOutputStream out) throws IOException {
        out.writeString(nvp.getName());
        out.writeString(nvp.getValue());
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        nvp = new BasicNameValuePair(in.readString(), in.readString());
    }

    private void readObjectNoData() throws ObjectStreamException {
        // nothing to do
    }
}

答案 1 :(得分:19)

我遇到了同样的问题,尝试在ArrayList中发送对象而不在Object类中实现Seiralizable。

快捷方案是在Bundle中包含ArrayList。

所以,在第一个活动中 - &gt;

ArrayList<Object> objects = new ArrayList<Object>(); 
// Object class does not implement Serializable interface

Bundle extra = new Bundle();
extra.putSerializable("objects", objects);

Intent intent = new Intent(getBaseContext(), ShowSpread.class);
intent.putExtra("extra", extra);

第二项活动 - &gt;

Bundle extra = getIntent().getBundleExtra("extra");
ArrayList<Object> objects = (ArrayList<Object>) extra.getSerializable("objects")

工作正常。

答案 2 :(得分:1)

您需要将Object数组作为Serializable传递给intent。 Object数组必须包含可串行化的对象。

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("first_name", first_name));
nameValuePairs.add(new BasicNameValuePair("last_name", last_name));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("password", password));

此处BaseNameValuePair必须是实现Serializable的类。使用toArray ArrayList进行放置,将类型转换放入Serializable。在getter Activity中,使用意图上的getSerializable方法。

答案 3 :(得分:0)

为此,List包含的通用类型必须实现Serializable

答案 4 :(得分:0)

这可以使用Parcelable界面实现。

  1. 让您的BasicNameValuePair课程实施Parcelable interface.see this以获取更多参考资料。

    public class BasicNameValuePair implements Parcelable{
    
    }
    
  2. 如上所述,将这些对象添加到ArrayList

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("first_name", first_name));
    nameValuePairs.add(new BasicNameValuePair("last_name", last_name));
    nameValuePairs.add(new BasicNameValuePair("email", email));
    nameValuePairs.add(new BasicNameValuePair("password", password));
    
  3. 使用intent将这些内容传递到putParcelableArrayListExtra()

    intent.putParcelableArrayListExtra ("nvp", nameValuePairs);
    startActivity(intent);