如何从另一个活动中读取方向更改

时间:2014-04-25 15:13:19

标签: android sharedpreferences screen-orientation

我制作了一个菜单按钮,用户可以在横向或纵向中更改方向。我的第一个活动与旋转相处得很好但是当我进入第二个活动时,活动的轮换变化和应用程序崩溃。

这是我的第一个活动:

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;
}

}

只要我参加第一次活动,一切都很好。但是当我转到第二个Activity时,方向会自动更改,而且应用程序崩溃会给我带来这个错误:

E / AndroidRuntime(1213):java.lang.RuntimeException:无法启动活动ComponentInfo {com.com.test / com.com.test.SecondActivity}:android.view.InflateException:二进制XML文件行#1:膨胀类错误

我的问题是我如何从第一个活动中读取SecondActivity中的共享偏好?

我尝试在SecondActivity中使用此代码:

private Context context; 

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

但到目前为止没有任何事情发生。当我转到SecondActivity时,应用程序崩溃了。

这是我的 SecondActivity

public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_activity);

}

感谢任何帮助!

0 个答案:

没有答案