将 bundle.getString() 的结果分配给 String 会抛出 ClassCastException
Assigning the result of bundle.getString() to a String throws a ClassCastException
我有一个名为 spiliaBeach
的 Place Object
实例,它包含您在下面看到的参数:
public PlaceObject spiliaBeach = new PlaceObject(R.string.spilia_beach,R.string.spilia_description,"2 Km from the Port",R.string.beach_category);
PlaceObject.java class:
private int name = 0;
private int description = 0;
private String locationDistance = "distance";
private int category = 0;
PlaceObject(int name, int description, String locationDistance, int category) {
this.name = name;
this.description = description;
this.locationDistance = locationDistance;
this.category = category;
}
在 MainActivity
中,我将数据通过 Bundle
传递到我的 DetailsActivity
,就像这样:
Intent detailsIntent = new Intent(this, DetailsActivity.class);
Bundle intentBundle = new Bundle();
intentBundle.putInt("name",beachDB.spiliaBeach.getName());
intentBundle.putInt("description",beachDB.spiliaBeach.getDescription());
intentBundle.putString("distance",beachDB.spiliaBeach.getLocationDistance());
//Log.d(TAG,"Location distance : " + beachDB.spiliaBeach.getLocationDistance());
//intentBundle.putInt("category",beachDB.spiliaBeach.getCategory());
detailsIntent.putExtra("data",intentBundle);
startActivity(detailsIntent);
并尝试在 DetailsActivity
的 onCreate()
中像这样检索它:
private void getIntentData(Intent intent) {
Bundle dataBundle = intent.getBundleExtra("data");
if(dataBundle != null) {
name = dataBundle.getInt(NAME_KEY);
description = dataBundle.getInt(DESC_KEY);
locationDistance = dataBundle.getString(LOC_DIST_KEY);
Log.d(TAG, "location distance : " + locationDistance + '\n' + "description : " + description);
} else {
Log.d(TAG,"Bundle is null");
}
}
但是 logcat 说 locationDistance
变量是 null
并且它抛出一个 ClassCastException
说当什么都没有时返回的字符串不能转换为整数直接在该行中的整数,对吗?有什么想法吗?
将您的 PlaceObject
class 声明为 Parcelable 并按照指南 https://www.vogella.com/tutorials/AndroidParcelable/article.html
实施所有必需的方法
然后您就可以将对象保存为
intentBundle.putParcelable(placeObject);
并将其检索为
Bundle dataBundle = getIntent().getExtras();
PlaceObject object = (PlaceObject) dataBundle.getParcelable(KEY);
编辑: 你传递 R.string.spilia_beach
这是整数 id,为了得到一个字符串使用 getString(R.string.spilia_beach) 等
我有一个名为 spiliaBeach
的 Place Object
实例,它包含您在下面看到的参数:
public PlaceObject spiliaBeach = new PlaceObject(R.string.spilia_beach,R.string.spilia_description,"2 Km from the Port",R.string.beach_category);
PlaceObject.java class:
private int name = 0;
private int description = 0;
private String locationDistance = "distance";
private int category = 0;
PlaceObject(int name, int description, String locationDistance, int category) {
this.name = name;
this.description = description;
this.locationDistance = locationDistance;
this.category = category;
}
在 MainActivity
中,我将数据通过 Bundle
传递到我的 DetailsActivity
,就像这样:
Intent detailsIntent = new Intent(this, DetailsActivity.class);
Bundle intentBundle = new Bundle();
intentBundle.putInt("name",beachDB.spiliaBeach.getName());
intentBundle.putInt("description",beachDB.spiliaBeach.getDescription());
intentBundle.putString("distance",beachDB.spiliaBeach.getLocationDistance());
//Log.d(TAG,"Location distance : " + beachDB.spiliaBeach.getLocationDistance());
//intentBundle.putInt("category",beachDB.spiliaBeach.getCategory());
detailsIntent.putExtra("data",intentBundle);
startActivity(detailsIntent);
并尝试在 DetailsActivity
的 onCreate()
中像这样检索它:
private void getIntentData(Intent intent) {
Bundle dataBundle = intent.getBundleExtra("data");
if(dataBundle != null) {
name = dataBundle.getInt(NAME_KEY);
description = dataBundle.getInt(DESC_KEY);
locationDistance = dataBundle.getString(LOC_DIST_KEY);
Log.d(TAG, "location distance : " + locationDistance + '\n' + "description : " + description);
} else {
Log.d(TAG,"Bundle is null");
}
}
但是 logcat 说 locationDistance
变量是 null
并且它抛出一个 ClassCastException
说当什么都没有时返回的字符串不能转换为整数直接在该行中的整数,对吗?有什么想法吗?
将您的 PlaceObject
class 声明为 Parcelable 并按照指南 https://www.vogella.com/tutorials/AndroidParcelable/article.html
然后您就可以将对象保存为
intentBundle.putParcelable(placeObject);
并将其检索为
Bundle dataBundle = getIntent().getExtras();
PlaceObject object = (PlaceObject) dataBundle.getParcelable(KEY);
编辑: 你传递 R.string.spilia_beach
这是整数 id,为了得到一个字符串使用 getString(R.string.spilia_beach) 等