有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

创建意图时,java方法getParcelableExtra()返回null

我在创建意图和使用getParcelableExtra()方法将对象移交给它时遇到了一个问题。 该项目的目标是创建回收者视图。如果选择了回收商的项目,则会启动新的意图,并应显示更详细的数据

因为数据是从外部MySQL数据库获取的,所以我使用截取来处理大多数网络内容

回收器在Volley onResponse()方法中实现,该方法首先在应用程序启动(onCreate)时调用。在这一点之前,一切正常,回收器正确加载和显示

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);
                }
            });

        }
    });

}

}

如您所见,我为名为onSuccess的Volley onResponse方法创建了一个接口。在这个方法中,我创建了一个监听器,这就是它变得丑陋的地方。 onItemClickListener打开该项的更详细视图,但方法getParcelableExtra()返回NULL。无论我做什么,它都不会返回食物类的对象

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);

}

}

因此putExtra()按预期工作,并获取食物类的正确对象。但是getParcelableExtra()每次都返回NULL。因此,启动意图中不显示任何值

食物类别:

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);

}
}

有人知道是什么导致getParcelableExtra()返回null吗

提前谢谢你的帮助


共 (1) 个答案