0

我喜欢从一个活动中传递另一个活动的对象。我实现了可序列化。但不知何故,该对象没有通过,在接收方我得到 NULL。请检查哪里错了?

public class TrackerInfo implements Serializable{
  /**
 * 
 */
private static final long serialVersionUID = 1L;
   String Idnumber;
  String Simcardnumber;
  String Description;
  String Model;
  String time;

  public String getSimcardnumber() {
      return Simcardnumber;
  }

  public String getDescription() {
      return Description;
  }

  public String getModel() {
      return Model;
  }
  public void setModel(String model) {
      this.Model = model;
  }

  public String getTime() {
      return time;
  }

  public void setInforFromCursor(Cursor cursor){
      this.Idnumber = cursor.getString(1);
      this.Simcardnumber = cursor.getString(2);
      this.Description = cursor.getString(3);
      this.Model = cursor.getString(4);
      this.time = cursor.getString(5);
  }}

在发送方,

@Override
    public void itemSelected(String id) {
        //get Tracker info
        dbHelper = new TrackerDBAdapter(this);
        dbHelper.open();
        Cursor cursor = dbHelper.fetchListByIDNum(id);
        TrackerInfo tracker = new TrackerInfo();
        tracker.setInforFromCursor(cursor);
        dbHelper.close();
        LinkedHashMap<String, Object> obj = new LinkedHashMap<String, Object>();
        obj.put("TRAKCER", tracker);
        Bundle b = new Bundle();
        b.putSerializable("bundleobj", obj);
        Intent intent = new Intent(this, DetailMapView.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("bundleobj", b);
        startActivity(intent);

    }

在接收端,

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    //setContentView(R.layout.activity_detail_map_view);
    try {  setContentView(R.layout.activity_detail_map_view);
         Bundle bn = new Bundle();
         bn = getIntent().getExtras();
         HashMap<String, Object> getobj = new HashMap<String, Object>();
         getobj = (HashMap<String, Object>) bn.getSerializable("bundleobj");
         trackerinfo = (TrackerInfo) getobj.get("TRACKER");
    } catch (Exception e) {
         Log.e("Err", e.getMessage());
    }}
4

2 回答 2

2

我认为你的问题在这里:

obj.put("TRAKCER", tracker);

您正在HashMap使用密钥添加跟踪器TRAKCER

但在这儿:

getobj.get("TRACKER");

您正在尝试使用键获取对象,TRACKER但键不相等,这是NPE. 你需要修复

obj.put("TRACKER", tracker);

现在键是相等的,所以它应该可以工作。

更新:

正如@Sam 指出的那样,您将您的包装HashMap成Bundle 并将Bundle 包装成Intent。因此,如果您的方法有效,您需要执行以下操作:

LinkedHashMap<String, Object> obj = new LinkedHashMap<String, Object>();
obj.put("TRACKER", tracker);

Bundle b = new Bundle();
b.putSerializable("trackerObj", obj);

Intent intent = new Intent(this, DetailMapView.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("bundleObj", b);

并用于检索HashMap

Bundle wrapper = getIntent().getBundleExtra("bundleObj"); // this Bundle contains HashMap
HashMap<String, Object> obj = (HashMap<String, Object>) wrapper.getSerializable("trackerObj");

推荐:

@Sam 如何再次指出,更好和更简洁的方法是将您的HashMapas Serializable 直接包装到 Intent withputExtra("trackerObj", obj)方法中,然后轻松将其检索为

HashMap<String, Object> o = (HashMap<String, Object>) getIntent().getSerializableExtra("trackerObj");
于 2013-03-29T16:29:05.820 回答
1

您有一些错误,请阅读 Sajmon 的答案,但您也将 Bundle 放入 Bundle 中,但只能从外部 Bundle 中读取。删除这样的代码:

Bundle b = new Bundle();
b.putSerializable("bundleobj", obj);

并将数据直接放入Intent中:

Intent intent = new Intent(this, DetailMapView.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("bundleobj", obj);

// You may need to help the compiler by casting it to a Serializable
//intent.putExtra("bundleobj", (Serializable) obj);

为了使用您的双 Bundle 方法,您需要获取两个“bundleobj”标签:

 bn = getIntent().getExtras().getBundle("bundleobj");
 HashMap<String, Object> getobj = (HashMap<String, Object>) bn.getSerializable("bundleobj");
于 2013-03-29T16:35:15.503 回答