将具有名称值对的arraylist从一个活动传递到另一个活动?

时间:2014-04-24 12:56:42

标签: android android-intent android-activity

我尝试将arraylist名称值对从一个活动传递给其他活动,但它给了我错误 日志堆栈就像这样

04-24 18:16:56.732: E/AndroidRuntime(1632): FATAL EXCEPTION: main
04-24 18:16:56.732: E/AndroidRuntime(1632): java.lang.RuntimeException: Parcel: unable to marshal value vehicle_model=dasd
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.os.Parcel.writeValue(Parcel.java:1235)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.os.Parcel.writeList(Parcel.java:622)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.os.Parcel.writeValue(Parcel.java:1195)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.os.Parcel.writeMapInternal(Parcel.java:591)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.os.Bundle.writeToParcel(Bundle.java:1619)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.os.Parcel.writeBundle(Parcel.java:605)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.content.Intent.writeToParcel(Intent.java:6470)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1741)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1411)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.app.Activity.startActivityForResult(Activity.java:3351)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.app.Activity.startActivityForResult(Activity.java:3312)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.app.Activity.startActivity(Activity.java:3522)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.app.Activity.startActivity(Activity.java:3490)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at com.example.jiffy.VehicleDetailsActivity.onClick(VehicleDetailsActivity.java:81)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.view.View.performClick(View.java:4084)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.view.View$PerformClick.run(View.java:16966)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.os.Handler.handleCallback(Handler.java:615)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.os.Looper.loop(Looper.java:137)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at java.lang.reflect.Method.invoke(Method.java:511)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-24 18:16:56.732: E/AndroidRuntime(1632):     at dalvik.system.NativeStart.main(Native Method)
04-24 18:16:58.922: I/Process(1632): Sending signal. PID: 1632 SIG: 9
04-24 18:16:59.892: E/Trace(1650): error opening trace file: No such file or directory (2)

我的代码就像这样

public class VehicleDetailsActivity extends Activity implements OnClickListener, Serializable {
    Button nextbutton;
    EditText vehiclemodel;
    EditText vehiclemake;
    EditText vehiclenumber;
    EditText seat;
    EditText kilometers;
    RadioGroup selectvehicle;
    RadioButton vehicletype;
    String vcategory;
    ArrayList<NameValuePair> vehicleinfo = new ArrayList<NameValuePair>();
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vehicledetails);
        setTitle("VEHICLE DETAILS");
        nextbutton = (Button) findViewById(R.id.next);
        nextbutton.setOnClickListener(this);
    }
@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.next:

        Bundle vehicledetails = new Bundle();
        vehiclemodel = (EditText) findViewById(R.id.vehiclemodel);
        vehiclemake = (EditText) findViewById(R.id.vehiclemake);
        vehiclenumber = (EditText) findViewById(R.id.vehiclenumber);
        seat = (EditText) findViewById(R.id.seat);
        kilometers = (EditText) findViewById(R.id.kilometer);
        selectvehicle = (RadioGroup) findViewById(R.id.selectvehicle);
        int selected_vehile = selectvehicle.getCheckedRadioButtonId();
        vehicletype = (RadioButton) findViewById(selected_vehile);

        if(vehicletype.getText() == "premium") {
            vcategory = "1";
        }
        else if(vehicletype.getText() == "Economy") {
            vcategory = "2";
        }
        else {
            vcategory = "3";
        }
        vehicleinfo.add(new BasicNameValuePair("vehicle_model", vehiclemodel.getText().toString()));    
        vehicleinfo.add(new BasicNameValuePair("vehicle_make", vehiclemake.getText().toString().trim()));
        vehicleinfo.add(new BasicNameValuePair("vehicle_number", vehiclenumber.getText().toString()));
        vehicleinfo.add(new BasicNameValuePair("seat", seat.getText().toString().trim()));
        vehicleinfo.add(new BasicNameValuePair("kilometer", kilometers.getText().toString().trim()));
        vehicleinfo.add(new BasicNameValuePair("vehicle_type", vcategory));
        Intent it = new Intent(this, SetLocationActivity.class);

          it.putExtra("vehicleinfo", vehicleinfo);
        startActivity(it);
        break;
    }
}
 }

我成功地能够通过bundle传递数据,但我想要数组列表,因为在下一个活动ican中添加新值并且可以将该arraylist传递给我的webservice。

1 个答案:

答案 0 :(得分:1)

扩展BasicNameValuePair类及其中的任何包含对象类(如果有)也&#34;实现Serializable&#34;:

public class Example extends BasicNameValuePair implements Serializable {

}