我的问题是: 当我打开对话框片段时,我将realm和realm结果从活动发送到对话框片段。我总是将不同的领域结果发送到对话框片段,这取决于我点击活动。这是对话框片段中的代码,当我点击打开对话框片段时,我从活动接收领域和realmResults:
public void setChangeNoteListener(ChangeNoteListener mChangeNoteListener, RealmResults<Drop> realmResults, Realm realm) {
mNotelistener = mChangeNoteListener;
mRealmResolts = realmResults;
mRealm = realm;
}
收到后,我用它们在对话框片段中显示一些数据。
问题是,当我改变应用程序方向时,应用程序会被粉碎,因为我不能再使用领域结果了。 我的问题是,如何使用onSaveInstanceStat保存整个领域结果,并在应用方向更改时,再次使用该领域结果。
请注意,在领域结果不仅是一个项目,还有更多项目。
编辑: 我用
从领域保存日期 @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);
}
当我加载app我加载这样的应用程序:
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();
}
}
当我旋转screan并且在这行代码上调用方法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)
答案 0 :(得分:0)
RealmResults<T>
表示从Realm实例获取的查询结果。
Realm是一个数据库。它存储东西。这是一个持久性解决方案。
如果Realm中有某些内容,则表示您不必在onSaveInstanceState()
中保留它,因为它已经存在于Realm文件中。
您应该只使用相同的RealmResults<T>
参数重新创建sort
,您应该没问题。
此外,在任何调用setChangeNoteListener
中寻找您的错误,并且不会初始化RealmResults。