我创建了一个按钮,用户可以更改横向或纵向的方向。 我的第一项活动与轮换相关。
这是我在共享偏好中使用的代码:
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:无法启动活动ComponentInfo {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);
}
感谢任何帮助!