1
votes

I'm doing parse the website using Jsoup.
I have a Map<String, String> cookies in MainActivity.
And I need to pass the value cookies to SecondActivity.
So I created a class implements Pracelable.

MainActivity class :

DataPasser dataPass = new DataPasser(id, pw, cookies);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("data", dataPass);
MainActivity.this.startActivity(intent);


SecondActivity class :

dataPasser dataPass = getIntent().getParcelableExtra("data");
String id = dataPass.getId();
String pw = dataPass.getPw();
Map<String, String> cookies = dataPass.getCookies();


the class implements Parcelable:

public class DataPasser implements Parcelable {

    private String id;
    private String pw;
    Map<String, String> cookies;

    public String getId() {
        return id;
    }

    public String getPw() {
        return pw;
    }

    public Map<String, String> getCookies() {
        return cookies;
    }

    public DataPasser(Parcel in) {
        this.id = in.readString();
        this.pw = in.readString();
        in.readMap(cookies, Map.class.getClassLoader()); /** NullPointerException here. **/
    }

    public DataPasser(String id, String pw, Map<String, String> cookies) {
        this.id = id;
        this.pw = pw;
        this.cookies = cookies;
    }


    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeString(id);
        out.writeString(pw);
        out.writeMap(cookies);        
    }

    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<DataPasser> CREATOR = new Parcelable.Creator<DataPasser>() {
        public DataPasser createFromParcel(Parcel in) {
            return new DataPasser(in);
        }
        public DataPasser[] newArray (int size) {
            return new DataPasser[size];
        }
    };    
}


Error Log :

FATAL EXCEPTION: main Process: com.hsk.hallymloginprac, PID: 8071 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hsk.hallymloginprac/com.hsk.hallymloginprac.MenuActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495) at android.app.ActivityThread.access$900(ActivityThread.java:170) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5635) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.os.Parcel.readMapInternal(Parcel.java:2305) at android.os.Parcel.readMap(Parcel.java:1575) at com.hsk.hallymloginprac.DataPasser.(DataPasser.java:37) at com.hsk.hallymloginprac.DataPasser$1.createFromParcel(DataPasser.java:83) at com.hsk.hallymloginprac.DataPasser$1.createFromParcel(DataPasser.java:81) at android.os.Parcel.readParcelable(Parcel.java:2111) at android.os.Parcel.readValue(Parcel.java:2020) at android.os.Parcel.readArrayMapInternal(Parcel.java:2321) at android.os.Bundle.unparcel(Bundle.java:249) at android.os.Bundle.getParcelable(Bundle.java:1206) at android.content.Intent.getParcelableExtra(Intent.java:5292) at com.hsk.hallymloginprac.MenuActivity.onCreate(MenuActivity.java:44) at android.app.Activity.performCreate(Activity.java:5580) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)  at android.app.ActivityThread.access$900(ActivityThread.java:170)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:146)  at android.app.ActivityThread.main(ActivityThread.java:5635)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)  at dalvik.system.NativeStart.main(Native Method) 

But, I got a NullPointerException in DataPasser class.
How can I pass Map<String, String> to another activity?

3
When you say It's because of Exception, Then it's always a good habit to post StackTrace.Shree Krishna
Have you initialised the map you are passing. Check any corner cases where the cookies array might be null.n.arrow001
@ShreeKrishna Okay. I didn't know that. I added StackTrace just now.Chris
What's your line 83 in DataPasser class?Mr Neo
@n.arrow001 I tried that. No Exception. But, cookies not passed to SecondActivity.Chris

3 Answers

0
votes

Another possible solution is to implement Serializable interface in DataPasser

public class DataPasser implements Serializable {

Pass data from MainActivity.java like you did

DataPasser dataPass = new DataPasser(id, pw, cookies);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("data", dataPass);
MainActivity.this.startActivity(intent);

And get same in SecondActivity.java like this

DataPasser dataPass = (DataPasser) getIntent().getExtras().get("data");

and then access elements of DataPasser.

It works fine for my pojo classes.

Hope it'll work.

Reference this also for Parcelable

1
votes

I couldn't identify your actual issue but what I can suggest is to use writeBundle instead of writeMap. As per the documentation too, It is strongly recommended to use writeBundle(Bundle) instead of this method, since the Bundle class provides a type-safe API that allows you to avoid mysterious type errors at the point of marshaling.

You can easily read the data using readBundle() which was written with writeBundle. It will Read and return a new Bundle object from the parcel at the current dataPosition(). If again the issue raised then please kindly comment.

UPDATE

As taken an example of Person, Simply your writeToParcel looks like this,

 @Override
    public void writeToParcel(Parcel dest, int flags) {
        // create a bundle for the key value pairs
        Bundle bundle = new Bundle();

        // insert the key value pairs to the bundle
        bundle.putString(KEY_NAME, name);
        bundle.putInt(KEY_AGE, age);

        // write the key value pairs to the parcel
        dest.writeBundle(bundle);
    }

And createFromParcel method looks like this,

@Override
        public Person createFromParcel(Parcel source) {
            // read the bundle containing key value pairs from the parcel
            Bundle bundle = source.readBundle();

            // instantiate a person using values from the bundle
            return new Person(bundle.getString(KEY_NAME),
                                bundle.getInt(KEY_AGE));
        }
0
votes

One more thing you can try. Try replacing

public DataPasser(Parcel in) {
    this.id = in.readString();
    this.pw = in.readString();
    in.readMap(cookies, Map.class.getClassLoader());
}

with

public DataPasser(Parcel in) {
    this.id = in.readString();
    this.pw = in.readString();
    in.readMap(cookies, String.class.getClassLoader()); 
}

Because, according to the documentation of Parcel.java, the method readMap is

public final void readMap(Map outVal, ClassLoader loader) {
    int N = readInt();
    readMapInternal(outVal, N, loader);
}

which calls

void readMapInternal(Map outVal, int N,
    ClassLoader loader) {
    while (N > 0) {
        Object key = readValue(loader);
        Object value = readValue(loader);
        outVal.put(key, value);
        N--;
    }
}

So here, readValue tries to load values of type map as 'key' and 'value' for map. readValue returns null hence values of 'key' and 'value' are null for

outVal.put(key,value)

Hence the NullPointerException