将包裹包裹的数组列表传递给其他活动?

时间:2019-05-17 06:04:09

标签: android android-intent arraylist parcelable

我有一台可包裹的类胆固醇监测仪。在我的活动中,我尝试通过意图将包裹性数组列表传递给另一个活动。我这样初始化监视器列表:

 private ArrayList<? extends Parcelable> monitor_list;

然后我通过它:

 monitor_list = patientListFragment.getMonitorList();
Intent intent = new Intent(this, CholesterolMonitorActivity.class);
            intent.putParcelableArrayListExtra("monitorList" ,monitor_list);
            startActivity(intent);

我的获取监视器列表方法返回胆固醇监视器的数组列表:

 public ArrayList<CholesterolMonitor> getMonitorList(){
    return this.monitorList;
}

在接收活动中,我有两个数组列表,

private ArrayList<? extends Parcelable> monitor_list;
private ArrayList<CholesterolMonitor>cholesterol_monitor;

monitor_list= this.getIntent().getParcelableArrayListExtra("monitorList");

 this.cholesterol_monitor = (ArrayList<CholesterolMonitor>) monitor_list;

但是,在启动此活动时应用程序崩溃了吗?这是错误日志。

Process: com.example.safeheart, PID: 22778
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List org.hl7.fhir.dstu3.model.Patient.getName()' on a null object reference
    at com.example.safeheart.patientList.MonitorListRecyclerAdapter.onBindViewHolder(MonitorListRecyclerAdapter.java:41)
    at com.example.safeheart.patientList.MonitorListRecyclerAdapter.onBindViewHolder(MonitorListRecyclerAdapter.java:20)
    at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder

1 个答案:

答案 0 :(得分:0)

您可以按照以下步骤进行操作

创建如下所示的Java对象

public class ClassMyModel implements Serializable {
   private ArrayList<CholesterolMonitor> cholesterolMonitorArrayList;

    public ArrayList<CholesterolMonitor> getCholesterolMonitorArrayList() {
        return cholesterolMonitorArrayList;
    }

    public void setCholesterolMonitorArrayList(ArrayList<CholesterolMonitor> cholesterolMonitorArrayList) {
        this.cholesterolMonitorArrayList = cholesterolMonitorArrayList;
    }
}

并在您的CholesterolMonitor类中实现Serializable

public class CholesterolMonitor implements Serializable {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

在活动A中

private ArrayList<CholesterolMonitor> cholesterolMonitorArrayList = new ArrayList<>();

            CholesterolMonitor test = new CholesterolMonitor();
            test.setName("test Name");
            cholesterolMonitorArrayList.add(test);

            ClassMyModel data = new ClassMyModel();
            data.setCholesterolMonitorArrayList(cholesterolMonitorArrayList);

            Intent intent = new Intent(SignUpActivity.this, ForgotPwdEmailActivity.class);
            intent.putExtra("TEST", data);
            startActivity(intent);

在活动B中

       if (getIntent() != null) {
            classMyModel = (ClassMyModel) getIntent().getSerializableExtra("TEST");
            if(classMyModel != null){
                System.out.println("Name :: "+ classMyModel.getCholesterolMonitorArrayList().get(0).getName());
            }
        }

注意:确保ClassMyModel的每个嵌套类都实现了Serializable接口