Android 如何阅读来自其他活动的方向更改

Android 如何阅读来自其他活动的方向更改,android,sharedpreferences,screen-orientation,Android,Sharedpreferences,Screen Orientation,我制作了一个菜单按钮,用户可以在横向或纵向中更改方向。我的第一个活动在旋转方面做得很好,但当我进入第二个活动时,活动的旋转会发生变化,应用程序会崩溃 这是我的第一个活动: public class MainActivity extends FragmentActivity { FragmentAdapter mAdapter; ViewPager mPager; PageIndicator mIndicator; int Number = 0; DecimalFormat df = new D

我制作了一个菜单按钮,用户可以在横向或纵向中更改方向。我的第一个活动在旋转方面做得很好,但当我进入第二个活动时,活动的旋转会发生变化,应用程序会崩溃

这是我的第一个活动:

public class MainActivity extends FragmentActivity {

FragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
int Number = 0;
DecimalFormat df = new DecimalFormat("@##,###,###,###");

/** SharedPreferences */
public static final String MyPREFERENCES = "MyPrefs";
SharedPreferences sharedpreferences;
int currentOrientation;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);

    /** Rotation SharedPreferences */
    SharedPreferences preferences = PreferenceManager
            .getDefaultSharedPreferences(this);
    int orienation = preferences.getInt("orientation", -1);
    if (orienation != -1) {
        setRequestedOrientation(orienation);
    }
    /** End */

    mAdapter = new FragmentAdapter(getSupportFragmentManager());

    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    mIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);

}

@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");

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menus, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        /** Rotation */
    case R.id.menuRotate:
        SharedPreferences preferences = PreferenceManager
        .getDefaultSharedPreferences(this);
        Editor editor = preferences.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();

        break;

    }
    return false;
}
}

只要我第一次参加活动,一切都很好。但当我进入第二个活动时,方向会自动改变,应用程序崩溃会给我带来以下错误:

E/AndroidRuntime1213:java.lang.RuntimeException:无法启动活动组件信息{com.com.test/com.com.test.SecondActivity}:android.view.InflateException:二进制XML文件第1行:膨胀类时出错

我的问题是,如何从第一个活动中读取第二个活动中的SharedReferences

我尝试在第二个活动中使用此代码:

private Context context; 

public void onCreate(){ 
 super.onCreate();
 context = getApplicationContext();
但到目前为止什么也没有发生。当我转到“第二个活动”时,应用程序崩溃,原因是维护更改

这是我的第二项活动:

private Context context; 

public void onCreate(){ 
 super.onCreate();
 context = getApplicationContext();

谢谢你的帮助

我不知道你想做什么,但崩溃发生在R.layout.second_活动中。根据InflateException,你可能使用了仅在values land或values port中的值,或者布局中拼写错误的某个类。如果第二个_活动在第一个位置自动运行,请尝试。假设我的第一个活动是纵向的,当我转到第二个_活动时,我的手机处于横向。应用程序会自动更改方向,因为我没有在第二个_活动中保存方向,它会自动从传感器获取方向,应用程序会崩溃。我只想从我的第一个活动中获取已保存的方向,并将其用于我的第二个活动。当第一个活动和手机处于相同的方向时,第二个活动可以正常工作,然后当我进入第二个活动时,什么都没有发生,并且工作正常。我认为第一个活动并不重要。如果打开第二个活动并更改方向,它将崩溃。就这些。搜索xml中的问题。