如何在android中将数据从一个活动发送到另一个活动?

时间:2014-10-25 21:19:41

标签: java android

我正在尝试将我的Web服务响应从一个活动发送到另一个活动。当我将一个活动移动到另一个活动而没有响应时它工作正常。但是当我发送响应时它会给我错误。请你帮我删除这个异常..

其实我这样发送:我认为这行中的问题 i.putExtra(“test”,data); 我喜欢这个..

10-25 21:13:47.619: E/AndroidRuntime(962): FATAL EXCEPTION: main
10-25 21:13:47.619: E/AndroidRuntime(962): java.lang.RuntimeException: Unable to start activity 

ComponentInfo{com.firstgroup.applicationload/com.firstgroup.ui.screens.Departuredashboardscreen}: java.lang.NullPointerException
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.os.Looper.loop(Looper.java:137)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.app.ActivityThread.main(ActivityThread.java:5041)
10-25 21:13:47.619: E/AndroidRuntime(962):  at java.lang.reflect.Method.invokeNative(Native Method)
10-25 21:13:47.619: E/AndroidRuntime(962):  at java.lang.reflect.Method.invoke(Method.java:511)
10-25 21:13:47.619: E/AndroidRuntime(962):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-25 21:13:47.619: E/AndroidRuntime(962):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-25 21:13:47.619: E/AndroidRuntime(962):  at dalvik.system.NativeStart.main(Native Method)
10-25 21:13:47.619: E/AndroidRuntime(962): Caused by: java.lang.NullPointerException
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.os.Parcel.readListInternal(Parcel.java:2237)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.os.Parcel.readList(Parcel.java:1531)
10-25 21:13:47.619: E/AndroidRuntime(962):  at com.firstgroup.dto.deparaturedaseboarddto.<init>(deparaturedaseboarddto.java:209)
10-25 21:13:47.619: E/AndroidRuntime(962):  at com.firstgroup.dto.deparaturedaseboarddto.<init>(deparaturedaseboarddto.java:193)
10-25 21:13:47.619: E/AndroidRuntime(962):  at com.firstgroup.dto.deparaturedaseboarddto$1.createFromParcel(deparaturedaseboarddto.java:185)
10-25 21:13:47.619: E/AndroidRuntime(962):  at com.firstgroup.dto.deparaturedaseboarddto$1.createFromParcel(deparaturedaseboarddto.java:1)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.os.Parcel.readParcelable(Parcel.java:2103)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.os.Parcel.readValue(Parcel.java:1965)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.os.Parcel.readMapInternal(Parcel.java:2226)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.os.Bundle.unparcel(Bundle.java:223)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.os.Bundle.getParcelable(Bundle.java:1173)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.content.Intent.getParcelableExtra(Intent.java:4330)
10-25 21:13:47.619: E/AndroidRuntime(962):  at com.firstgroup.ui.screens.Departuredashboardscreen.onCreate(Departuredashboardscreen.java:21)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.app.Activity.performCreate(Activity.java:5104)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-25 21:13:47.619: E/AndroidRuntime(962):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-25 21:13:47.619: E/AndroidRuntime(962):  ... 11 more




@Override
public void getWebserviceResponse(String result) {
    // TODO Auto-generated method stub
    //JSONObject js=new Jso(result);
    deparaturedaseboarddto data = new Gson().fromJson(result, deparaturedaseboarddto.class);
    /*listView=(ListView)findViewById(R.id.listView1);
    adapter = new CustomListAdapter(this,  data.getData());
     listView.setAdapter(adapter);*/
    Intent i = new Intent(Appliacationload.this,Departuredashboardscreen.class);
    i.putExtra("test", data);
    startActivity(i);
    finish();
    Log.d("----", "========");
}

第二项活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.departure_dashboard);
    Intent i = getIntent();
    deparaturedaseboarddto myParcelableObject = (deparaturedaseboarddto) i.getParcelableExtra("test");
    Log.d("===", "hhhh");

}

**Model class :**




public class deparaturedaseboarddto  implements Parcelable{
     ArrayList<deparaturedaseboarddto> data;

        public ArrayList<deparaturedaseboarddto> getData() {
            return data;
        }

        public void setData(ArrayList<deparaturedaseboarddto> data) {
            this.data = data;
        }

    @SerializedName("alertsId")
 int alertsId;
    @SerializedName("destExpArrival")
 String destExpArrival;
    @SerializedName("destSchArrival")
 String destSchArrival;
    @SerializedName("expDepart")
 String expDepart;
    @SerializedName("filteredStation")
 String filteredStation;
    @SerializedName("platformNo")
 String platformNo;
    @SerializedName("rid")
 String rid;
    @SerializedName("schDepart")
 String schDepart;
    @SerializedName("toc")
 String toc;
    @SerializedName("toExpArrival")
 String toExpArrival;
    @SerializedName("toSchArrival")
 String toSchArrival;
    @SerializedName("trainID")
 String trainID;
    @SerializedName("trainLastReportedAt")
 String trainLastReportedAt;
    @SerializedName("destinationStation")
 DestinationStation destinationStation;
    public deparaturedaseboarddto(String trainID,String toc,String trainLastReportedAt, String platformNo, String schDepart, String expDepart, int alertsId, String rid, String destSchArrival, String filteredStation, String destExpArrival, String toSchArrival, String toExpArrival,DestinationStation destinationStation){
        super();
        this.trainID=trainID;
        this.toc=toc;
        this.trainLastReportedAt=trainLastReportedAt;
        this.platformNo=platformNo;
        this.schDepart=schDepart;
        this.expDepart=expDepart;
        this.alertsId=alertsId;
        this.destinationStation=destinationStation;
        this.toExpArrival=toExpArrival;
        this.toSchArrival=toSchArrival;
        this.destExpArrival=destExpArrival;
        this.filteredStation=filteredStation;
        this.destSchArrival=destSchArrival;
        this.rid=rid;

    }
public DestinationStation getDestinationStation() {
    return destinationStation;
}
public void setDestinationStation(DestinationStation destinationStation) {
    this.destinationStation = destinationStation;
}
public int getAlertsId() {
    return alertsId;
}
public void setAlertsId(int alertsId) {
    this.alertsId = alertsId;
}
public String getDestExpArrival() {
    return destExpArrival;
}
public void setDestExpArrival(String destExpArrival) {
    this.destExpArrival = destExpArrival;
}
public String getDestSchArrival() {
    return destSchArrival;
}
public void setDestSchArrival(String destSchArrival) {
    this.destSchArrival = destSchArrival;
}
public String getExpDepart() {
    return expDepart;
}
public void setExpDepart(String expDepart) {
    this.expDepart = expDepart;
}
public String getFilteredStation() {
    return filteredStation;
}
public void setFilteredStation(String filteredStation) {
    this.filteredStation = filteredStation;
}
public String getPlatformNo() {
    return platformNo;
}
public void setPlatformNo(String platformNo) {
    this.platformNo = platformNo;
}
public String getRid() {
    return rid;
}
public void setRid(String rid) {
    this.rid = rid;
}
public String getSchDepart() {
    return schDepart;
}
public void setSchDepart(String schDepart) {
    this.schDepart = schDepart;
}
public String getToc() {
    return toc;
}
public void setToc(String toc) {
    this.toc = toc;
}
public String getToExpArrival() {
    return toExpArrival;
}
public void setToExpArrival(String toExpArrival) {
    this.toExpArrival = toExpArrival;
}
public String getToSchArrival() {
    return toSchArrival;
}
public void setToSchArrival(String toSchArrival) {
    this.toSchArrival = toSchArrival;
}
public String getTrainID() {
    return trainID;
}
public void setTrainID(String trainID) {
    this.trainID = trainID;
}
public String getTrainLastReportedAt() {
    return trainLastReportedAt;
}
public void setTrainLastReportedAt(String trainLastReportedAt) {
    this.trainLastReportedAt = trainLastReportedAt;
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(alertsId);
dest.writeString(destExpArrival);
dest.writeString(destSchArrival);

dest.writeString(expDepart);
dest.writeString(filteredStation);

dest.writeString(platformNo);
dest.writeString(rid);
dest.writeString(schDepart);
dest.writeString(toc);
dest.writeString(toExpArrival);
dest.writeString(toSchArrival);
dest.writeString(trainID);
dest.writeString(trainLastReportedAt);
dest.writeParcelable(this.destinationStation, flags);
dest.writeList(data);

}

    public static final Parcelable.Creator<deparaturedaseboarddto> CREATOR = new Parcelable.Creator<deparaturedaseboarddto>() {
        public deparaturedaseboarddto createFromParcel(Parcel in) {
            return new deparaturedaseboarddto(in);
        }

        public deparaturedaseboarddto[] newArray(int size) {
            return new deparaturedaseboarddto[size];
        }
    };

    private deparaturedaseboarddto(Parcel in) {
        this.alertsId=in.readInt();
        this.destExpArrival=in.readString();
        this.destSchArrival=in.readString();
        this.expDepart=in.readString();
        this.filteredStation=in.readString();
        this.platformNo=in.readString();
        this.rid=in.readString();
        this.schDepart=in.readString();
        this.toc=in.readString();
        this.toExpArrival=in.readString();
        this.toSchArrival=in.readString();
        this.trainID=in.readString();
        this.trainLastReportedAt=in.readString();
        this.destinationStation = in.readParcelable(DestinationStation.class.getClassLoader());

              in.readList(data,deparaturedaseboarddto.class.getClassLoader());
    }
}


package com.firstgroup.dto;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;


public class DestinationStation implements Parcelable {
    @SerializedName("crsCode")
 String crsCode;
    @SerializedName("stationName")
 String stationName;
public DestinationStation(String crsCode, String stationName) {
        // TODO Auto-generated constructor stub
    super();
    this.crsCode=crsCode;
    this.stationName=stationName;
    }
public String getCrsCode() {
    return crsCode;
}
public void setCrsCode(String crsCode) {
    this.crsCode = crsCode;
}
public String getStationName() {
    return stationName;
}
public void setStationName(String stationName) {
    this.stationName = stationName;
}
@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(crsCode);
    dest.writeString(stationName);  
}

public static final Parcelable.Creator<DestinationStation> CREATOR = new Parcelable.Creator<DestinationStation>() {
    public DestinationStation createFromParcel(Parcel in) {
        return new DestinationStation(in);
    }

    public DestinationStation[] newArray(int size) {
        return new DestinationStation[size];
    }
};

private DestinationStation(Parcel in) {

    this.crsCode=in.readString();
    this.stationName=in.readString();
}
}

更新了一个..

@Override
public void getWebserviceResponse(String result) {
    // TODO Auto-generated method stub
    //JSONObject js=new Jso(result);
    deparaturedaseboarddto data = new Gson().fromJson(result, deparaturedaseboarddto.class);
    /*listView=(ListView)findViewById(R.id.listView1);
    adapter = new CustomListAdapter(this,  data.getData());
     listView.setAdapter(adapter);*/
    Intent i = new Intent(Appliacationload.this,Departuredashboardscreen.class);

    EventBus.getDefault().postSticky(data);

    //i.putExtra("test", (Parcelable)data);
    startActivity(i);
    finish();
    Log.d("----", "========");
}

下一个活动:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.departure_dashboard);
    Intent i = getIntent();
    deparaturedaseboarddto myParcelableObject = (deparaturedaseboarddto) EventBus.getDefault().getStickyEvent(deparaturedaseboarddto.class);   
    Log.d("===", "hhhh");

}

例外:

10-26 00:56:36.052: E/AndroidRuntime(806): FATAL EXCEPTION: AsyncTask #3
10-26 00:56:36.052: E/AndroidRuntime(806): java.lang.RuntimeException: An error occured while executing doInBackground()
10-26 00:56:36.052: E/AndroidRuntime(806):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-26 00:56:36.052: E/AndroidRuntime(806):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-26 00:56:36.052: E/AndroidRuntime(806):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-26 00:56:36.052: E/AndroidRuntime(806):  at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-26 00:56:36.052: E/AndroidRuntime(806):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-26 00:56:36.052: E/AndroidRuntime(806):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-26 00:56:36.052: E/AndroidRuntime(806):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-26 00:56:36.052: E/AndroidRuntime(806):  at java.lang.Thread.run(Thread.java:856)
10-26 00:56:36.052: E/AndroidRuntime(806): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
10-26 00:56:36.052: E/AndroidRuntime(806):  at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
10-26 00:56:36.052: E/AndroidRuntime(806):  at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
10-26 00:56:36.052: E/AndroidRuntime(806):  at android.view.View.requestLayout(View.java:15473)
10-26 00:56:36.052: E/AndroidRuntime(806):  at android.view.View.setFlags(View.java:8444)
10-26 00:56:36.052: E/AndroidRuntime(806):  at android.view.View.setVisibility(View.java:5716)
10-26 00:56:36.052: E/AndroidRuntime(806):  at android.app.Dialog.hide(Dialog.java:294)
10-26 00:56:36.052: E/AndroidRuntime(806):  at com.firstgroup.webservice.RequestTask.doInBackground(RequestTask.java:66)
10-26 00:56:36.052: E/AndroidRuntime(806):  at com.firstgroup.webservice.RequestTask.doInBackground(RequestTask.java:1)
10-26 00:56:36.052: E/AndroidRuntime(806):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-26 00:56:36.052: E/AndroidRuntime(806):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-26 00:56:36.052: E/AndroidRuntime(806):  ... 4 more
10-26 00:56:40.424: E/WindowManager(806): Activity com.firstgroup.applicationload.Appliacationload has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40f6b308 G.E..... R.....ID 0,0-729,324} that was originally added here
10-26 00:56:40.424: E/WindowManager(806): android.view.WindowLeaked: Activity com.firstgroup.applicationload.Appliacationload has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40f6b308 G.E..... R.....ID 0,0-729,324} that was originally added here
10-26 00:56:40.424: E/WindowManager(806):   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
10-26 00:56:40.424: E/WindowManager(806):   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
10-26 00:56:40.424: E/WindowManager(806):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
10-26 00:56:40.424: E/WindowManager(806):   at android.app.Dialog.show(Dialog.java:281)
10-26 00:56:40.424: E/WindowManager(806):   at com.firstgroup.webservice.RequestTask.onPreExecute(RequestTask.java:36)
10-26 00:56:40.424: E/WindowManager(806):   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
10-26 00:56:40.424: E/WindowManager(806):   at android.os.AsyncTask.execute(AsyncTask.java:534)
10-26 00:56:40.424: E/WindowManager(806):   at com.firstgroup.applicationload.Appliacationload.calldeparutureWebservice(Appliacationload.java:130)
10-26 00:56:40.424: E/WindowManager(806):   at com.firstgroup.applicationload.Appliacationload.access$1(Appliacationload.java:126)
10-26 00:56:40.424: E/WindowManager(806):   at com.firstgroup.applicationload.Appliacationload$2.onClick(Appliacationload.java:91)
10-26 00:56:40.424: E/WindowManager(806):   at android.view.View.performClick(View.java:4204)
10-26 00:56:40.424: E/WindowManager(806):   at android.view.View$PerformClick.run(View.java:17355)
10-26 00:56:40.424: E/WindowManager(806):   at android.os.Handler.handleCallback(Handler.java:725)
10-26 00:56:40.424: E/WindowManager(806):   at android.os.Handler.dispatchMessage(Handler.java:92)
10-26 00:56:40.424: E/WindowManager(806):   at android.os.Looper.loop(Looper.java:137)
10-26 00:56:40.424: E/WindowManager(806):   at android.app.ActivityThread.main(ActivityThread.java:5041)
10-26 00:56:40.424: E/WindowManager(806):   at java.lang.reflect.Method.invokeNative(Native Method)
10-26 00:56:40.424: E/WindowManager(806):   at java.lang.reflect.Method.invoke(Method.java:511)
10-26 00:56:40.424: E/WindowManager(806):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-26 00:56:40.424: E/WindowManager(806):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-26 00:56:40.424: E/WindowManager(806):   at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

应该是这样的:

@Override
    public void getWebserviceResponse(String result) {
        // TODO Auto-generated method stub
        //JSONObject js=new Jso(result);
        deparaturedaseboarddto data = new Gson().fromJson(result, deparaturedaseboarddto.class);
        /*listView=(ListView)findViewById(R.id.listView1);
        adapter = new CustomListAdapter(this,  data.getData());
         listView.setAdapter(adapter);*/
        Intent i = new Intent(Appliacationload.this,Departuredashboardscreen.class);
        //i.putExtra("test", data);
        EventBus.getDefault().postSticky(deparaturedaseboarddto);
        startActivity(i);
        finish();
        Log.d("----", "========");
    }

和Departuredashboardscreen

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.departure_dashboard);

    deparaturedaseboarddto myParcelableObject = EventBus.getDefault().getStickyEvent(deparaturedaseboarddto.class);   
    Log.d("===", "hhhh");

}

答案 1 :(得分:0)

尝试使用getExtra()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.departure_dashboard);
    //use getExtra()
    Intent i = getIntent().getExtras();
    deparaturedaseboarddto myParcelableObject =
                              (deparaturedaseboarddto) i.getParcelableExtra("test");
    Log.d("===", "hhhh");
}