gson.fromJson(getIntent()。getStringExtra()返回null

时间:2019-08-09 11:00:12

标签: android string android-intent gson

我正在从SurfaceInspectionListActivityAddSurfaceInspectionActivity发送对象

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

4 个答案:

答案 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)

尝试一下:

  1. 使用Gson将对象转换为JSON
  2. 将其传递给活动
  3. 在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。