我正在从SurfaceInspectionListActivity
向AddSurfaceInspectionActivity
发送对象
在putExtra上,我可以在调试模式下查看数据,但是在检索数据时却出现NULL
我试图以String的形式获取它以进行测试,结果也导致NULL
surfaceObjString = getIntent().getStringExtra("myjsonSurfaceObject2");
这是SurfaceInspectionListActivity
意图代码
public void addSurfaceInspection(View view) {
Intent intent = new Intent(SurfaceInspectionListActivity.this, AddSurfaceInspectionActivity.class);
intent.putExtra("myjsonSurfaceObject2", surfaceObject);
startActivity(intent);
finish();
}
以下是AddSurfaceInspectionActivity
上的检索代码
Gson gson = new Gson();
surfaceObj = gson.fromJson(getIntent().getStringExtra("myjsonSurfaceObject2"), Surfaces.class);
答案 0 :(得分:0)
尝试:
public void addSurfaceInspection(View view) {
Intent intent = new Intent(SurfaceInspectionListActivity.this, AddSurfaceInspectionActivity.class);
Gson gson = new Gson();
String sruface = gson.toJson(surfaceObject, Surfaces.class);
intent.putExtra("myjsonSurfaceObject2", sruface );
startActivity(intent);
finish();
}
答案 1 :(得分:0)
首先通过工具Surfaces
接口制作Serializable
Serializable
。然后更改SurfaceInspectionListActivity
:
public void addSurfaceInspection(View view) {
Intent intent = new Intent(SurfaceInspectionListActivity.this, AddSurfaceInspectionActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("myjsonSurfaceObject2", surfaceObject);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
为了Surface
中的AddSurfaceInspectionActivity
对象:
Surface surface = getIntent().getSerializableExtra("myjsonSurfaceObject2");
答案 2 :(得分:0)
尝试一下:
在SurfaceInspectionListActivity中,使用Gson将其转换回对象
public void addSurfaceInspection(View view) {
Intent intent = new Intent(SurfaceInspectionListActivity.this, AddSurfaceInspectionActivity.class);
//Convert to string
Gson gson = new Gson();
String json = gson.toJson(surfaceObject);
intent.putStringExtra("myjsonSurfaceObject2", json);
startActivity(intent);
finish();
}
在AddSurfaceInspectionActivity
中:
Gson gson = new Gson();
surfaceObj = gson.fromJson(getIntent().getStringExtra("myjsonSurfaceObject2"), Surfaces.class);
答案 3 :(得分:0)
如果您已经在使用已实现Serializable
的对象,则无需再使用Gson。只需使用putExtra()
和getExtra()
。像这样:
// there is nothing need to be changed here.
public void addSurfaceInspection(View view) {
Intent intent = new Intent(SurfaceInspectionListActivity.this, AddSurfaceInspectionActivity.class);
intent.putExtra("myjsonSurfaceObject2", surfaceObject);
startActivity(intent);
finish();
}
然后使用以下方法获取对象:
Bundle bundle = intent.getExtra();
surfaceObj = (Surfaces) bundle.getSerializable("myjsonSurfaceObject2");
如果 Gson 是您的先决条件,则可以使用以下方式:
// get the String of object
public static String convertToGsonString(Surface surfaceObject) {
Gson gson = new Gson();
return gson.toJson(surfaceObject);
}
// convert to object from String
public static Surface convertToObjectFromString(String objString) {
Type type = new TypeToken<Surface>() {}.getType();
Gson gson = new Gson();
return gson.fromJson(objString, type);
}
然后您可以使用上述方法发送意图:
public void addSurfaceInspection(View view) {
Intent intent = new Intent(SurfaceInspectionListActivity.this, AddSurfaceInspectionActivity.class);
intent.putExtra("myjsonSurfaceObject2", convertToGsonString(surfaceObject));
startActivity(intent);
finish();
}
然后收到它:
Bundle extra = getIntent().getExtra();
String objString = extra.getString("myjsonSurfaceObject2");
surfaceObj = convertToObjectFromString(objString);
请注意,使用Gson时不需要使用Serializable。