【问题标题】:gson.fromJson(getIntent().getStringExtra() returns nullgson.fromJson(getIntent().getStringExtra() 返回 null
【发布时间】:2019-08-09 11:00:12
【问题描述】:

我正在将对象从SurfaceInspectionListActivity 发送到AddSurfaceInspectionActivity

在 putExtra 上我可以在调试模式下看到数据,但是在检索数据时我得到 NULL

我尝试将其作为字符串获取,以进行也导致 NULL 的测试

surfaceObjString = getIntent().getStringExtra("myjsonSurfaceObject2");

这是SurfaceInspectionListActivity Intent 代码

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

【问题讨论】:

标签: android string android-intent gson


【解决方案1】:

试试看:

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

【讨论】:

  • 在开始下一个活动之前没有工作崩溃
  • 显示日志猫
  • @YashKrishanVerma 。类型为 in.gov.bihar.minorirrigation.mwrd.mwrd.db.entity.Surfaces 的预期接收者,但得到了 java.lang.String
  • 我已经添加了我的答案,请检查。
【解决方案2】:

首先通过实现Serializable接口使SurfacesSerializable。然后改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");

【讨论】:

  • 公共类 Surfaces 实现了 Serializable 已经在那里尝试过这段代码 .. 没有崩溃,但对象在其他活动中为 NULL
【解决方案3】:

试试这个:

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

【讨论】:

  • 这里的“用户”是什么?
  • 应用程序在启动活动 logCat 之前崩溃 原因:java.lang.IllegalArgumentException:预期接收器类型为 in.gov.bihar.minorirrigation.mwrd.mwrd.db.entity.Surfaces,但得到了 java.lang .String at java.lang.reflect.Field.get(Native Method)
  • ok 代替 putExtra 使用 putStringExtra,我已在答案中添加
  • 你试过使用 putstringextra 吗?
  • intent.putStringExtra 给出了 IDE 无法识别的错误
【解决方案4】:

如果您已经在使用实现了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);
}

那么就可以使用上面的方法发送intent了:

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。

【讨论】:

  • intent.getExtra(); // 给出错误 Bundle bundle = getIntent().getExtras(); surfaceObj = (Surfaces) bundle.getSerializable("myjsonSurfaceObject2");试过这个,但仍然得到 NULL
  • 你需要检查你是否有多余的。做这样的事情:Bundle bundle = getIntent().getExtras(); if(bundle.has("myjsonSurfaceObject2")) { // process extra here. } else { // Not found any extra here!!! }
  • 这个问题通常是因为你给额外的密钥不正确。
  • intent.putExtra("myjsonSurfaceObject2", surfaceObject);捆绑包 = getIntent().getExtras(); surfaceObj = (Surfaces) bundle.getSerializable("myjsonSurfaceObject2");没用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-20
相关资源
最近更新 更多