Android的如何保存的onSaveInstanceState与境界结果屏幕旋转(Android

2019-09-28 03:37发布

我的问题是这样的:当我打开对话框片段,我送的境界和境界结果从活动对话片段。 我总是发送不同的境界结果对话框片段,这取决于我点击活动。 这是在对话片段代码,我收到的境界和活动,当我点击打开的对话框片段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);
    }

当我加载应用程序我加载的应用程序是这样的:

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) 

Answer 1:

RealmResults<T>表示查询结果您从领域实例获得。

境界是一个数据库。 它存储的东西。 这是一个持久的解决方案。

如果有东西在王国,那么这意味着你不必坚持在它onSaveInstanceState()因为它已经在领域的文件。

你应该重新创建RealmResults<T>与同sort参数,你应该罚款。

此外,寻求你的错误在任何调用setChangeNoteListener和不初始化RealmResults。



文章来源: Android how to save realm results with onSaveInstanceState for screen rotation