Java 使用哈希表在活动之间传递数据
我正在为Android开发一个笔记应用程序。 为了减轻在活动之间共享值(数据/对象)的痛苦,我编写了一个IntentHelper类,它在Java 使用哈希表在活动之间传递数据,java,android,nullpointerexception,Java,Android,Nullpointerexception,我正在为Android开发一个笔记应用程序。 为了减轻在活动之间共享值(数据/对象)的痛苦,我编写了一个IntentHelper类,它在哈希表中存储对 public class IntentHelper { private static IntentHelper _instance; private static Hashtable<String, Object> _hash; private IntentHelper() { _hash
哈希表中存储
对
public class IntentHelper {
private static IntentHelper _instance;
private static Hashtable<String, Object> _hash;
private IntentHelper() {
_hash = new Hashtable<>();
}
public static IntentHelper getInstance() {
if (_instance == null) {
_instance = new IntentHelper();
}
return _instance;
}
public static void addObjectForKey(Object obj, String key) {
getInstance()._hash.put(key, obj);
}
public static Object getObjectForKey(String key) {
IntentHelper helper = getInstance();
Object data = helper._hash.get(key);
helper._hash.remove(key);
helper = null;
return data;
}
}
当我调试应用程序时,我可以看到从IntentThelper检索到的Note
对象不是空的。以下是两项活动:
main活动
public class MainActivity extends AppCompatActivity {
Note note;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.single_note_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
reDrawNoteList();
}
private void reDrawNoteList() {
Cursor cursor = Utils.readFromDatabase(this, Constants.SELECT_ALL_NOTES);
ListView notesList = (ListView) findViewById(R.id.noteList);
NoteCursorAdapter noteCursorAdapter = new NoteCursorAdapter(this, cursor);
notesList.setAdapter(noteCursorAdapter);
notesList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
note = (Note) view.getTag();
startNoteViewActivity();
}
});
}
@Override
protected void onResume() {
super.onResume();
reDrawNoteList();
}
private void startNoteViewActivity(){
IntentHelper.getInstance().addObjectForKey(note, Constants.SINGLE_NOTE);
Intent note_view_intent = new Intent(MainActivity.this, NoteViewActivity.class);
startActivity(note_view_intent);
}
}
public class NoteViewActivity extends AppCompatActivity {
Note note;
private CustomTextView noteTitle;
private CustomTextView noteText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_view);
Toolbar toolbar = (Toolbar) findViewById(R.id.single_note_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
noteTitle = (CustomTextView) findViewById(R.id.tv_NoteDescription);
noteText = (CustomTextView) findViewById(R.id.tv_NoteText);
reDrawView();
}
private void reDrawView(){
note = (Note) IntentHelper.getObjectForKey(Constants.SINGLE_NOTE);
noteTitle.setText(note.getNoteDescription());
noteText.setText(note.getNoteText());
}
@Override
protected void onResume() {
super.onResume();
reDrawView();
}
}
我知道问题在于在检索时删除
对,因为我可以通过注释掉以下内容来阻止应用程序崩溃:
helper._hash.remove(key);
这个问题发生在新发布的应用程序(onCreate
)和OnResume
)上。我可以理解为什么它发生在OnResume
中,而不是OnCreate
中
有人能在这里解释一下实际的问题吗?问题是,当发生创建时删除对象,然后尝试启动其他活动或使用哈希表中的该键返回到某个活动。此时,对象将从哈希表中消失
您可以使用Parcelable
对象,并在活动或Intent中使用onSaveInstanceState
和onRestoreInstanceState
方法。putExtra
传递Parcelable
s单例哈希表似乎不是一个好主意。使用包裹在两个位置之间正确移动数据intents@cricket_007singleton hashtable有什么特别的问题吗?singleton是一个反模式的东西。另外,你已经有了数据库,所以你有了一个持久层there@cricket_007不建议使用持久层来保存要在意图之间共享的数据。这不是我的观点。不管怎么说,您使用的单例模式是错误的getInstance()
应该在您需要的类中调用一次,因此IntentThelper.getInstance()
一次,然后在该实例上调用getObjectForKey
。对于\u实例
变量,您当前的代码实际上毫无意义,因为\u散列
本身也是静态的
helper._hash.remove(key);