简体   繁体   中英

Parcelable Object causing error

I had created a class like below, that implements Parcelable

package com.fyrweel.cityinfo;

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

public class JSONParser implements Parcelable  {

    public String fcodeName;
    public String toponymName;
    public String countrycode;
    public String fcl;
    public String fclName;
    public String name;
    public String wikipedia;
    public Double lng;
    public Double lat;
    public String fcode;
    public Long geonameId;
    public Long population;

     public int describeContents() {
            return 0;
        }

        // write your object's data to the passed-in Parcel
        public void writeToParcel(Parcel out, int flags) {
            out.writeString(fcodeName);
            out.writeString(toponymName);
            out.writeString(countrycode);
            out.writeString(fcl);
            out.writeString(fclName);
            out.writeString(name);
            out.writeString(wikipedia);
            out.writeString(fcode);
            out.writeLong(geonameId);
            out.writeLong(population);
            out.writeDouble(lng);
            out.writeDouble(lat);
        }

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

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

        JSONParser(Parcel in) {
            fcodeName = in.readString();
            toponymName = in.readString();
            countrycode = in.readString();
            fcl = in.readString();
            fclName = in.readString();
            name = in.readString();
            wikipedia = in.readString();
            fcode = in.readString();
            geonameId = in.readLong();
            population = in.readLong();
            lng = in.readDouble();
            lat = in.readDouble();
        }

}

Then I tried to pass an object of this class from one activity to another using intents. In the first activity i used the following code:

Intent detailsActivityLauncher = new Intent(MainActivity.this, DetailsActivity.class);
detailsActivityLauncher.putExtra("SelectedCityDetails", (Parcelable)aCity);
startActivityForResult(detailsActivityLauncher, 0);

And in the second activity the following code to receive it:

Intent recievedIntent = getIntent();        
JSONParser theCity = (JSONParser)recievedIntent.getParcelableExtra("SelectedCityDetails");

And when the second activity loads, get the following error in LogCat:

04-15 15:50:03.680: E/AndroidRuntime(2958): FATAL EXCEPTION: main
04-15 15:50:03.680: E/AndroidRuntime(2958): Process: com.fyrweel.cityinfo, PID: 2958
04-15 15:50:03.680: E/AndroidRuntime(2958): java.lang.NullPointerException
04-15 15:50:03.680: E/AndroidRuntime(2958):     at com.fyrweel.cityinfo.JSONParser.<init>(JSONParser.java:55)
04-15 15:50:03.680: E/AndroidRuntime(2958):     at com.fyrweel.cityinfo.MainActivity$1.onSuccess(MainActivity.java:66)
04-15 15:50:03.680: E/AndroidRuntime(2958):     at com.loopj.android.http.AsyncHttpResponseHandler.onSuccess(AsyncHttpResponseHandler.java:232)
04-15 15:50:03.680: E/AndroidRuntime(2958):     at com.loopj.android.http.AsyncHttpResponseHandler.onSuccess(AsyncHttpResponseHandler.java:220)
04-15 15:50:03.680: E/AndroidRuntime(2958):     at com.loopj.android.http.AsyncHttpResponseHandler.onSuccess(AsyncHttpResponseHandler.java:245)
04-15 15:50:03.680: E/AndroidRuntime(2958):     at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:365)
04-15 15:50:03.680: E/AndroidRuntime(2958):     at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:135)
04-15 15:50:03.680: E/AndroidRuntime(2958):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 15:50:03.680: E/AndroidRuntime(2958):     at android.os.Looper.loop(Looper.java:136)
04-15 15:50:03.680: E/AndroidRuntime(2958):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-15 15:50:03.680: E/AndroidRuntime(2958):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 15:50:03.680: E/AndroidRuntime(2958):     at java.lang.reflect.Method.invoke(Method.java:515)
04-15 15:50:03.680: E/AndroidRuntime(2958):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-15 15:50:03.680: E/AndroidRuntime(2958):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-15 15:50:03.680: E/AndroidRuntime(2958):     at dalvik.system.NativeStart.main(Native Method)

Line 55 of JSONParser.java is:

fcodeName = in.readString();

Why is this happening? How to resolve this?

try this:

JSONParser theCity = (JSONParser) getIntent().getExtras().getParcelable("SelectedCityDetails");

Also

detailsActivityLauncher.putExtra("SelectedCityDetails", aCity);

hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM