在我的onCreate
的{{1}}方法中,我按如下方式加载home片段:
main activity
旋转屏幕时,由于HomeFragment fragment = new HomeFragment().setData(data); // data is a List<XYZ>
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.bottom_navigation_frame, fragment);
transaction.commit();
为空,应用会以onCreateView
的{{1}}方法崩溃。
我不确定何时应该保存和恢复HomeFragment
的状态,data
是从数据库加载的大对象。尝试保存时可能会收到另一个错误。那么如何旋转屏幕时应该解决空指针问题?
答案 0 :(得分:0)
当配置更改或屏幕旋转时,片段丢失其数据。 因此您必须在onPause上卸载片段,然后在屏幕更改配置时重新加载数据和片段。这是屏幕方向更改的监听器。
m_sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.i(TAG,"Orientation : PORTRAIT");
}
else {
Log.i(TAG,"Orientation : LANDSCAPE");
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);
sm.registerListener(m_sensorEventListener, sm.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR), SensorManager.SENSOR_DELAY_NORMAL);
答案 1 :(得分:-1)
将此代码添加到Manifest.xml中的Current Activity的属性中
M =[(1, 1), (5, 6), (0, 0)]
1) print([any(x) for x in M])
[True, True, False] #only the last tuple does not have any true element
2) print([all(x) for x in M])
[True, True, False] #all elements of the last tuple are not true
3) print([not all(x) for x in M])
[False, False, True] #NOT operator applied to 2)
4) print([any(x) and not all(x) for x in M])
[False, False, False] #AND operator applied to 1) and 3)
# if we had M =[(1, 1), (5, 6), (1, 0)], we could get [False, False, True] in 4)
# because the last tuple satisfies both conditions: any of its elements is TRUE
#and not all elements are TRUE