问题描述
我的问题是这样的:当我打开对话框片段时,我将活动的领域和领域结果发送到对话框片段.我总是将不同的领域结果发送到对话框片段,具体取决于我在活动中单击的内容.这是对话框片段中的代码,当我单击以打开对话框片段时,我从活动中接收领域和realmResults:
My problem is this:When i open dialog fragment, i send realm and realm results from activity to dialog fragment. I always send different realm results to dialog fragment, depending what i click in activity. This is code in dialog fragment where i receive realm and realmResults from activity, when i click to open dialog fragment:
public void setChangeNoteListener(ChangeNoteListener mChangeNoteListener, RealmResults<Drop> realmResults, Realm realm) {
mNotelistener = mChangeNoteListener;
mRealmResolts = realmResults;
mRealm = realm;
}
我收到它们后,便用它们在对话框片段中显示一些数据.
After i receive them, i use them for showing some data in dialog fragment.
问题是当我更改应用程序方向时,应用程序被压碎了,因为我无法再使用领域结果.我的问题是,如何使用onSaveInstanceStat保存整个领域结果,以及当应用程序方向发生更改时,如何再次使用该领域结果.
Problem is when i change app orientation, app get crushed because i can not use realm results anymore.My question is, how to save the whole realm results with onSaveInstanceStat and when app orientation change, to use that realm results again.
请注意,领域中的结果不仅是一项,而且还有更多项.
Note that in realm results is not only one item, there is more items.
我使用
@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);
}
当我加载应用程序时,我会这样加载应用程序:
When i load app i load app like this:
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()时,应用程序会崩溃(当我尝试从领域获取数据时).
App get crushed when i rotate screan and and call method setTitleNoteTextToDialogNote() on this line of code (when i try to get data from realm).
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实例获得的查询结果.
RealmResults<T>
represents the query results you obtain from the Realm instance.
领域是一个数据库.它存储东西.这是一个持久解决方案.
Realm is a database. It stores things. It's a persistence solution.
如果Realm中存在某些内容,则意味着您不必将其保留在onSaveInstanceState()
中,因为它已经存在于Realm文件中.
If there is something in the Realm, then that means you don't have to persist it in onSaveInstanceState()
, because it's already in the Realm file.
您只需使用相同的sort
参数重新创建RealmResults<T>
,就可以了.
You should just recreate the RealmResults<T>
with the same sort
parameter and you should be fine.
此外,请使用任何调用setChangeNoteListener
的方式查找错误,并且不要初始化RealmResults.
Also, seek your bug in whatever calls setChangeNoteListener
and doesn't initialize the RealmResults.
这篇关于Android如何使用onSaveInstanceState保存领域结果以进行屏幕旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!