Android 如何使用屏幕旋转的 onSaveInstanceState 保存领域结果
Android how to save realm results with onSaveInstanceState for screen rotation
我的问题是:
当我打开对话片段时,我将 activity 中的领域和领域结果发送到对话片段。我总是将不同的领域结果发送到对话框片段,具体取决于我在 activity 中单击的内容。这是对话框片段中的代码,当我单击打开对话框片段时,我从 activity 接收领域和 realmResults:
public void setChangeNoteListener(ChangeNoteListener mChangeNoteListener, RealmResults<Drop> realmResults, Realm realm) {
mNotelistener = mChangeNoteListener;
mRealmResolts = realmResults;
mRealm = realm;
}
收到它们后,我用它们在对话框片段中显示一些数据。
问题是当我改变应用程序方向时,应用程序被压碎,因为我不能再使用领域结果了。
我的问题是,如何使用 onSaveInstanceStat 保存整个领域结果,以及当应用程序方向改变时,再次使用该领域结果。
注意realm results不是只有一项,还有更多项。
编辑:
我用
从领域保存日期
@Override
public void onSaveInstanceState(Bundle outState) {
Bundle argumetns = getArguments();
int position = argumetns.getInt("POSITION");
outState.putString("myTitle", String.valueOf(mRealmResolts.get(position).getWhat()));
outState.putString("myNote", String.valueOf(mRealmResolts.get(position).getWhat_note()));
super.onSaveInstanceState(outState);
}
当我加载应用程序时,我会像这样加载应用程序:
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mBtnClose = (ImageButton) view.findViewById(R.id.btn_close_note);
mBtnDelete = (ImageButton) view.findViewById(R.id.dialog_btn_delete_note);
mEditTextTitle = (EditText) view.findViewById(R.id.dialog_text_naslov);
mEditTextNote = (EditText) view.findViewById(R.id.dialog_text_note);
mBtnEdit = (Button) view.findViewById(R.id.dialog_btn_edit);
mBtnCompleted = (Button) view.findViewById(R.id.dialog_btn_completed);
mBtnSave = (Button) view.findViewById(R.id.dialog_btn_save);
mBtnForward = (ImageButton) view.findViewById(R.id.dialog_btn_forward);
mBucketNoteTime = (BucketPickerView) view.findViewById(R.id.bpv_date_in_note);
mBucketNoteTimeNonSelected = (BucketPickerView_Unselected) view.findViewById(R.id.bpv_date_in_note_unselected);
mSwitchButton = (Switch) view.findViewById(R.id.switchButtonNote);
mSwitchChangeDate = (Switch) view.findViewById(R.id.switchChangeDate);
mSwitchNoteText = (TextView) view.findViewById(R.id.switchTextAbboveNote);
dialogPickerButtonNote = (ImageView) view.findViewById(R.id.dialog_picker_button_note);
mBtnClose.setOnClickListener(this);
mBtnDelete.setOnClickListener(this);
mBtnEdit.setOnClickListener(this);
mBtnCompleted.setOnClickListener(this);
mBtnSave.setOnClickListener(this);
mBtnForward.setOnClickListener(this);
dialogPickerButtonNote.setOnClickListener(this);
if (savedInstanceState != null) {
Log.d("realmtest", "onViewCreated: prvi");
String mojStringTitle = savedInstanceState.getString("myTitle");
String mojStringNote = savedInstanceState.getString("myNote");
mEditTextTitle.setText(mojStringTitle);
mEditTextNote.setText(mojStringNote);
buttonWhenSaveVisibility();
setTitleNoteTextToDialogNote();
}
else {
buttonWhenSaveVisibility();
setTitleNoteTextToDialogNote();
}
}
当我旋转屏幕并在这行代码中调用方法 setTitleNoteTextToDialogNote() 时(当我尝试从领域获取数据时)应用程序被压碎。
mEditTextTitle.setText(mRealmResolts.get(position).getWhat());
这是错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'io.realm.RealmModel io.realm.RealmResults.get(int)' on a null object reference
at com.petar.android.simplenote.DialogNote.setTitleNoteTextToDialogNote(DialogNote.java:231)
at com.petar.android.simplenote.DialogNote.onViewCreated(DialogNote.java:134)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1127)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1272)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2149)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:600)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at com.petar.android.simplenote.ActivityMain.onStart(ActivityMain.java:296)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
at android.app.Activity.performStart(Activity.java:6696)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4519)
at android.app.ActivityThread.-wrap19(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
RealmResults<T>
代表你从Realm实例中得到的查询结果。
Realm 是一个数据库。它存储东西。这是一个持久性解决方案。
如果 Realm 中有内容,则意味着您不必将其保存在 onSaveInstanceState()
中,因为它已经在 Realm 文件中。
您应该使用相同的 sort
参数重新创建 RealmResults<T>
,应该没问题。
此外,请在任何调用 setChangeNoteListener
且未初始化 RealmResults 的情况下查找错误。
我的问题是: 当我打开对话片段时,我将 activity 中的领域和领域结果发送到对话片段。我总是将不同的领域结果发送到对话框片段,具体取决于我在 activity 中单击的内容。这是对话框片段中的代码,当我单击打开对话框片段时,我从 activity 接收领域和 realmResults:
public void setChangeNoteListener(ChangeNoteListener mChangeNoteListener, RealmResults<Drop> realmResults, Realm realm) {
mNotelistener = mChangeNoteListener;
mRealmResolts = realmResults;
mRealm = realm;
}
收到它们后,我用它们在对话框片段中显示一些数据。
问题是当我改变应用程序方向时,应用程序被压碎,因为我不能再使用领域结果了。 我的问题是,如何使用 onSaveInstanceStat 保存整个领域结果,以及当应用程序方向改变时,再次使用该领域结果。
注意realm results不是只有一项,还有更多项。
编辑: 我用
从领域保存日期 @Override
public void onSaveInstanceState(Bundle outState) {
Bundle argumetns = getArguments();
int position = argumetns.getInt("POSITION");
outState.putString("myTitle", String.valueOf(mRealmResolts.get(position).getWhat()));
outState.putString("myNote", String.valueOf(mRealmResolts.get(position).getWhat_note()));
super.onSaveInstanceState(outState);
}
当我加载应用程序时,我会像这样加载应用程序:
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mBtnClose = (ImageButton) view.findViewById(R.id.btn_close_note);
mBtnDelete = (ImageButton) view.findViewById(R.id.dialog_btn_delete_note);
mEditTextTitle = (EditText) view.findViewById(R.id.dialog_text_naslov);
mEditTextNote = (EditText) view.findViewById(R.id.dialog_text_note);
mBtnEdit = (Button) view.findViewById(R.id.dialog_btn_edit);
mBtnCompleted = (Button) view.findViewById(R.id.dialog_btn_completed);
mBtnSave = (Button) view.findViewById(R.id.dialog_btn_save);
mBtnForward = (ImageButton) view.findViewById(R.id.dialog_btn_forward);
mBucketNoteTime = (BucketPickerView) view.findViewById(R.id.bpv_date_in_note);
mBucketNoteTimeNonSelected = (BucketPickerView_Unselected) view.findViewById(R.id.bpv_date_in_note_unselected);
mSwitchButton = (Switch) view.findViewById(R.id.switchButtonNote);
mSwitchChangeDate = (Switch) view.findViewById(R.id.switchChangeDate);
mSwitchNoteText = (TextView) view.findViewById(R.id.switchTextAbboveNote);
dialogPickerButtonNote = (ImageView) view.findViewById(R.id.dialog_picker_button_note);
mBtnClose.setOnClickListener(this);
mBtnDelete.setOnClickListener(this);
mBtnEdit.setOnClickListener(this);
mBtnCompleted.setOnClickListener(this);
mBtnSave.setOnClickListener(this);
mBtnForward.setOnClickListener(this);
dialogPickerButtonNote.setOnClickListener(this);
if (savedInstanceState != null) {
Log.d("realmtest", "onViewCreated: prvi");
String mojStringTitle = savedInstanceState.getString("myTitle");
String mojStringNote = savedInstanceState.getString("myNote");
mEditTextTitle.setText(mojStringTitle);
mEditTextNote.setText(mojStringNote);
buttonWhenSaveVisibility();
setTitleNoteTextToDialogNote();
}
else {
buttonWhenSaveVisibility();
setTitleNoteTextToDialogNote();
}
}
当我旋转屏幕并在这行代码中调用方法 setTitleNoteTextToDialogNote() 时(当我尝试从领域获取数据时)应用程序被压碎。
mEditTextTitle.setText(mRealmResolts.get(position).getWhat());
这是错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'io.realm.RealmModel io.realm.RealmResults.get(int)' on a null object reference
at com.petar.android.simplenote.DialogNote.setTitleNoteTextToDialogNote(DialogNote.java:231)
at com.petar.android.simplenote.DialogNote.onViewCreated(DialogNote.java:134)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1127)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1272)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2149)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:600)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at com.petar.android.simplenote.ActivityMain.onStart(ActivityMain.java:296)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
at android.app.Activity.performStart(Activity.java:6696)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4519)
at android.app.ActivityThread.-wrap19(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
RealmResults<T>
代表你从Realm实例中得到的查询结果。
Realm 是一个数据库。它存储东西。这是一个持久性解决方案。
如果 Realm 中有内容,则意味着您不必将其保存在 onSaveInstanceState()
中,因为它已经在 Realm 文件中。
您应该使用相同的 sort
参数重新创建 RealmResults<T>
,应该没问题。
此外,请在任何调用 setChangeNoteListener
且未初始化 RealmResults 的情况下查找错误。