Android onRestoreInstanceState-屏幕方向更改后如何还原对象?

时间:2019-03-14 18:27:37

标签: android

我正在尝试编写我的第一个Android应用程序。我有3个片段,每个片段都有相同的UI,并且它是自己的名为Rota#number的对象。 我有一个典型的初学者问题:屏幕方向更改后如何保留我的对象?我想还原Rota1-Rota3对象,getKryptonim()和Rota类的getter仅用于测试。我收到的不是还原的对象,
在空对象引用上'java.lang.String com.firetrap.android.firetrapair.Rota.getKryptonim()'

主要活动是:

public class MainActivity extends AppCompatActivity {

private static final String TAG = "FireTrapAir";

private Button mRota1_button;
private Button mRota2_button;
private Button mRota3_button;

private Rota mRota1;
private Rota mRota2;
private Rota mRota3;

private Fragment fragmentR1;
private Fragment fragmentR2;
private Fragment fragmentR3;

public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Log.d(TAG,"onSaveInstanceState()");
    outState.putSerializable("ROTA1", mRota1);
    outState.putSerializable("ROTA2", mRota2);
    outState.putSerializable("ROTA3", mRota3);

}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG,"onCreate()");
    setContentView(R.layout.activity_main);

    final FragmentManager fm = getSupportFragmentManager();

        Rota mRota1 = new Rota();
        Rota mRota2 = new Rota();
        Rota mRota3 = new Rota();


    Fragment fragmentR1 = RotaFragment.newInstance(mRota1);
    Fragment fragmentR2 = RotaFragment.newInstance(mRota2);
    Fragment fragmentR3 = RotaFragment.newInstance(mRota3);

    fm.beginTransaction().add(R.id.r1dane_container, fragmentR1).commit();
    fm.beginTransaction().add(R.id.r2dane_container, fragmentR2).commit();
    fm.beginTransaction().add(R.id.r3dane_container, fragmentR3).commit();
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    Log.d(TAG, "onRestoreInstanceState()");

    if (savedInstanceState != null) {
        Rota mRota1 = (Rota) savedInstanceState.getSerializable("ROTA1");
        Log.d(TAG, "onRestoreInstanceState(): ROTA1" + mRota1.getKryptonim());
        Rota mRota2 = (Rota) savedInstanceState.getSerializable("ROTA2");
        Log.d(TAG, "onRestoreInstanceState(): ROTA2" + mRota2.getKryptonim());
        Rota mRota3 = (Rota) savedInstanceState.getSerializable("ROTA3");
        Log.d(TAG, "onRestoreInstanceState(): ROTA3" + mRota3.getKryptonim());
    }
}

}

RotaFragment显示Rota类属性。

Rota类为:

public class Rota implements Serializable {
    private UUID mId;
    private String mKryptonim;
    private String mStrazak1;
    private String mStrazak2;
    private String mMiejsceDzialan;
    private String mKanalRadiowy;
    private String mKanalKRG;
    private Integer mCisnienieWejscia;
    private Date mWejscie;

    public Rota() {

        Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault());

        mId = UUID.randomUUID();
        mWejscie = new Date();
        mKryptonim = "podaj kryptonim";
        mStrazak1 = "strażak1";
        mStrazak2 = "strażak2";
        mMiejsceDzialan = "miejsce działań";
        mKanalRadiowy = "B004";
        mKanalKRG = "KRG 1";
        mCisnienieWejscia = 0;
        mWejscie = localCalendar.getTime();
    }
.... getters and setters
}

1 个答案:

答案 0 :(得分:0)

问题:在您的活动班级中,有3个班级变量

private Rota mRota1;
private Rota mRota2;
private Rota mRota3;

但是在onCreate()方法中,您创建了3个具有相同名称的局部变量

Rota mRota1 = new Rota();
Rota mRota2 = new Rota();
Rota mRota3 = new Rota();

所以3个类变量始终为null,这就是为什么您会收到错误消息。

解决方案::将活动代码更改为

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG,"onCreate()");
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        mRota1 = new Rota();
        mRota2 = new Rota();
        mRota3 = new Rota();
    } else {
        mRota1 = (Rota) savedInstanceState.getSerializable("ROTA1");
        Log.d(TAG, "onRestoreInstanceState(): ROTA1" + mRota1.getKryptonim());
        mRota2 = (Rota) savedInstanceState.getSerializable("ROTA2");
        Log.d(TAG, "onRestoreInstanceState(): ROTA2" + mRota2.getKryptonim());
        mRota3 = (Rota) savedInstanceState.getSerializable("ROTA3");
        Log.d(TAG, "onRestoreInstanceState(): ROTA3" + mRota3.getKryptonim());
    }

    final FragmentManager fm = getSupportFragmentManager();
    Fragment fragmentR1 = RotaFragment.newInstance(mRota1);
    Fragment fragmentR2 = RotaFragment.newInstance(mRota2);
    Fragment fragmentR3 = RotaFragment.newInstance(mRota3);

    fm.beginTransaction().add(R.id.r1dane_container, fragmentR1).commit();
    fm.beginTransaction().add(R.id.r2dane_container, fragmentR2).commit();
    fm.beginTransaction().add(R.id.r3dane_container, fragmentR3).commit();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Log.d(TAG,"onSaveInstanceState()");
    outState.putSerializable("ROTA1", mRota1);
    outState.putSerializable("ROTA2", mRota2);
    outState.putSerializable("ROTA3", mRota3);
}