stoepfel0711
stoepfel0711

Reputation: 13

Method getParcelableExtra() returns null when creating an intent

I have a problem regarding the creation of an intent and the handover of an object with the getParcelableExtra() method to it. The aim of the project is the creation of a recycler view. If an item from the recycler is selected a new intent gets started and should display more detailed data.

Because the data is fetched from an external MySQL DB I'm using Volley for most of the networking stuff.

The recycler is implemented inside the Volley onResponse() method which is first called at app start (onCreate). Until this point everything works fine and the recycler is loaded and displayed correctly.

public class UserAreaActivity extends AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_area);

    initializeRecycler();
}

    public void initializeRecycler() {

    operations.getFoodFromDB(getFoodID(), new IDatabaseOperations() {
        @Override
        public void onSuccess(final ArrayList<Food> food_list) {

            mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
            mLayoutmanager = new LinearLayoutManager(UserAreaActivity.this);
            mAdapter = new FoodAdapter(food_list);
            mRecyclerView.setLayoutManager(mLayoutmanager);
            mRecyclerView.setAdapter(mAdapter);

            mAdapter.setOnItemClickListener(new FoodAdapter.OnItemClickListener() {
                @Override
                public void OnItemClick(int position) {
                    Intent intent = new Intent(UserAreaActivity.this, FoodProfile.class);
 GETS CORRECT OBJECT----->intent.putExtra("food", food_list.get(position));
                    startActivity(intent);
                }
            });

        }
    });

}

}

As you see I created an interface for the Volley onResponse method called onSuccess. Inside this method I am creating an onItemClickListener and this is where it gets ugly. The onItemClickListener opens up the more detailed view of the item, but the method getParcelableExtra() returns NULL. Whatever I do it never returns an object of the class Food.

public class FoodProfile extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(activity_food_profile);

    Food food = getIntent().getParcelableExtra("food");<----RETURNS NULL 


    String price = food.getPrice();
    String name = food.getName();
    String rating = ((Float)food.getRating()).toString();
    String imageRes = food.getmimgId();


    TextView mPrice = (TextView) findViewById(R.id.Price);
    mPrice.setText(price);
    TextView mName = (TextView) findViewById(R.id.Name);
    mName.setText(name);
    TextView mRating = (TextView) findViewById(R.id.Rating);
    mRating.setText(rating);

}

}

So the putExtra() works as intended and gets the correct object of the food class. But getParcelableExtra() returns NULL everytime. So no value is displayed in the started intent.

Food class:

public class Food implements Parcelable {

public int id;
public String name;
public String category;
public String date;
public int vegan;
public int vegetarian;
public String price;
public String uuid;
public float rating;
public String mimgId;

public Food(int id, String name, String category, int vegan, int vegetarian, String price, String uuid, float rating, String mimgId){
    this.id = id;
    this.name = name;
    this.category = category;
    this.date = date;
    this.vegan = vegan;
    this.vegetarian = vegetarian;
    this.price = price;
    this.uuid = uuid;
    this.rating = rating;
    this.mimgId = mimgId;
}

protected Food(Parcel in) {
    id = in.readInt();
    name = in.readString();
    category = in.readString();
    date = in.readString();
    vegan = in.readInt();
    vegetarian = in.readInt();
    price = in.readString();
    uuid = in.readString();
    rating = in.readFloat();
    mimgId = in.readString();
}

public static final Creator<Food> CREATOR = new Creator<Food>() {
    @Override
    public Food createFromParcel(Parcel in) {
        return new Food(in);
    }

    @Override
    public Food[] newArray(int size) {
        return new Food[size];
    }
};

public int getId() {
    return id;
}

public String getName() {
    if(name != null) {
        name = name.replaceAll(System.getProperty("line.separator"), (""));
    }
    return name;
}

public String getDate() {
    return date;
}

public int isVegan() {
    return vegan;
}

public int isVegetarian() {
    return vegetarian;
}

public String getPrice() {
    return price;
}

public String getUuid() {
    return uuid;
}

public float getRating(){return rating;}

public String getmimgId() {
    return mimgId;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(price);
    dest.writeString(name);
    dest.writeString(((Float)rating).toString());
    dest.writeString(mimgId);

}
}

Has anyone an idea whats causing the getParcelableExtra() to return null?

Thanks for your help in advance!

Upvotes: 1

Views: 3737

Answers (1)

Elvison
Elvison

Reputation: 163

As I commented above, the writeToParcel() is problematic. The parcel should be written and read in the same order.

Please refer to the following pages as reference:

What is Parcelable in android

Using Parcelable

Understanding Androids Parcelable - Tutorial

Upvotes: 5

Related Questions