Android 阅读其他活动中的方向更改
我做了一个按钮,用户可以改变横向或纵向的方向。 我的第一项活动在轮换中做得很好 这是我在共享首选项中编写的代码:Android 阅读其他活动中的方向更改,android,orientation,Android,Orientation,我做了一个按钮,用户可以改变横向或纵向的方向。 我的第一项活动在轮换中做得很好 这是我在共享首选项中编写的代码: SharedPreferences sharedpreferences = getSharedPreferences( MyPREFERENCES, Context.MODE_PRIVATE); Editor editor = sharedpreferences.edit(); if (getR
SharedPreferences sharedpreferences = getSharedPreferences(
MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
editor.putInt("orientation",
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
editor.putInt("orientation",
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
editor.commit();
下面是我读到的:
SharedPreferences sharedpreferences = getSharedPreferences(
MyPREFERENCES, Context.MODE_PRIVATE);
int orientation = sharedpreferences.getInt("orientation", -1);
if (orientation != -1) {
setRequestedOrientation(orientation);
}
以下是onSave和onRestore:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (outState == null) {
outState = new Bundle();
}
outState.putInt("currentOrientation",
Integer.valueOf(currentOrientation));
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
currentOrientation = savedInstanceState.getInt("currentOrientation");
}
除了我要参加另一项活动,我的应用程序在那里崩溃外,其他一切都很正常
这给了我一个错误:
E/AndroidRuntime(1213):java.lang.RuntimeException:无法启动活动组件信息{com.com.test/com.com.test.test}:android.view.InflateException:二进制XML文件行#1:错误膨胀类
我的问题是,如何从我的第一个活动中阅读第二个活动中的共享参考资料
编辑:
这是我的第二项活动
public class Test extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
}
谢谢你的帮助 您是否调用
editor.commit()
将更改写入磁盘?是的,我的第一个活动(写入和读取)没有问题。:)