iShare
iShare

Reputation: 125

How Parcelable work in Android? Null Pointer Exception

I have wriiten following code to pass the value to activity

  Intent intent = new Intent(DetailActivity.this,
                        MyMapActivity.class);
        intent.putExtra("tourguide.intent.extra.isbusiness", false);

        startActivity(intent);

And in the receiving end i have following code

boolean IsBusiness = false;
    IsBusiness = getIntent().getParcelableExtra(
        "tourguide.intent.extra.isbusiness");

I am getting NullPointerException error

I even tried with CASTING as follow

 IsBusiness = (boolean)getIntent().getParcelableExtra(
        "tourguide.intent.extra.isbusiness");

But no use. :(

Upvotes: 1

Views: 1488

Answers (4)

Artemis
Artemis

Reputation: 4851

Just pass boolean value instaed of String one. Threre is no need for parcelable item. In you case

Intent intent = new Intent(DetailActivity.this,
                        TourMapActivity.class);
intent.putExtra("isBusiness", false);

startActivity(intent);

You can pass you entities serializing them into plain types using Parselable mechanizm.

Here is the first example form Google for using Parselable.

public class ObjectA implements Parcelable {

private String strValue;
private Integer intValue;

/**
 * Standard basic constructor for non-parcel
 * object creation
 */
public ObjectA() { ; };

/**
 *
 * Constructor to use when re-constructing object
 * from a parcel
 *
 * @param in a parcel from which to read this object
 */
public ObjectA(Parcel in) {
    readFromParcel(in);
}

/**
 * standard getter
 *
 * @return strValue
 */
public String getStrValue() {
    return strValue;
}

/**
 * Standard setter
 *
 * @param strValue
 */
public void setStrValue(String strValue) {
    this.strValue = strValue;
}

/**
 * standard getter
 *
 * @return
 */
public Integer getIntValue() {
    return intValue;
}

/**
 * Standard setter
 *
 * @param intValue
 */
public void setIntValue(Integer intValue) {
    this.intValue = intValue;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

    // We just need to write each field into the
    // parcel. When we read from parcel, they
    // will come back in the same order
    dest.writeString(strValue);
    dest.writeInt(intValue);
}

/**
 *
 * Called from the constructor to create this
 * object from a parcel.
 *
 * @param in parcel from which to re-create object
 */
private void readFromParcel(Parcel in) {

    // We just need to read back each
    // field in the order that it was
    // written to the parcel
    strValue = in.readString();
    intValue = in.readInt();
}

/**
 *
 * This field is needed for Android to be able to
 * create new objects, individually or as arrays.
 *
 * This also means that you can use use the default
 * constructor to create the object and use another
 * method to hyrdate it as necessary.
 *
 * I just find it easier to use the constructor.
 * It makes sense for the way my brain thinks ;-)
 *
 */
public static final Parcelable.Creator CREATOR =
    new Parcelable.Creator() {
        public ObjectA createFromParcel(Parcel in) {
            return new ObjectA(in);
        }

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

}

Upvotes: 1

Dixit Patel
Dixit Patel

Reputation: 3192

I think this Code will help full to you

Pass to Activity:

Intent i = new Intent(getBaseContext(), NameOfActivity.class);
i.putExtra("my_boolean_key", myBooleanVariable);
startActivity(i)

Retrieve in Second Activity:

Bundle bundle = getIntent().getExtras();
boolean myBooleanVariable = bundle.getBoolean("my_boolean_key");

Upvotes: 0

Andro Selva
Andro Selva

Reputation: 54322

Try

  getIntent().getBooleanExtra(name, defaultValue);

Something like,

 IsBusiness = getIntent().getBooleanExtra("tourguide.intent.extra.isbusiness", false);

Upvotes: 3

Codesen
Codesen

Reputation: 7832

First, get the intent which has started your activity using the getIntent() method:

Intent intent = getIntent();

IsBusiness = intent.getBooleanExtra("mauritiustourguide.intent.extra.isbusiness");

Upvotes: 1

Related Questions