我有我的MainActivity,我扩展了FragmentActivity。 我还有其他3个片段活动,我使用此代码而不是onCreate片段的原因(我将我的应用程序从Activity更改为Fragment)。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
以下是我如何扩展我的活动:
public class MainActivity extends FragmentActivity {
FragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
int Number = 0;
private TextView mCalculatorDisplay;
private Calculation mCalculation;
DecimalFormat df = new DecimalFormat("@##,###,###,###");
/** SharedPreferences */
public static final String MyPREFERENCES = "MyPrefs";
SharedPreferences sharedpreferences;
int currentOrientation;
这是我在MainActivity中的onCreate:
@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);
mAdapter = new FragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
这里是onSaveInstanceState:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putDouble("OPERAND", mCalculation.getResult());
outState.putDouble("MEMORY", mCalculation.getMemory());
outState.putInt("currentOrientation", Integer.valueOf(currentOrientation));
}
这里是onRestoreInstanceState:
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mCalculation.setOperand(savedInstanceState.getDouble("OPERAND"));
mCalculation.setMemory(savedInstanceState.getDouble("MEMORY"));
mCalculatorDisplay.setText(df.format(mCalculation.getResult()));
currentOrientation = savedInstanceState.getInt("currentOrientation");
}
最后这是我使用旋转按钮的菜单:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuAbout:
startActivity(new Intent("com.this.calculation.ABOUT"));
return true;
/** 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 / AndroidRuntime(467):at com.this.calculation.MainActivity.onSaveInstanceState(MainActivity.java:60)
更新日志:
03-08 13:35:50.998: W/dalvikvm(623): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
03-08 13:35:51.068: E/AndroidRuntime(623): FATAL EXCEPTION: main
03-08 13:35:51.068: E/AndroidRuntime(623): java.lang.NullPointerException
03-08 13:35:51.068: E/AndroidRuntime(623): at mini.worlld.ecalculation.MainActivity.onSaveInstanceState(MainActivity.java:59)
03-08 13:35:51.068: E/AndroidRuntime(623): at android.app.Activity.performSaveInstanceState(Activity.java:1113)
03-08 13:35:51.068: E/AndroidRuntime(623): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1185)
03-08 13:35:51.068: E/AndroidRuntime(623): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3325)
03-08 13:35:51.068: E/AndroidRuntime(623): at android.app.ActivityThread.access$700(ActivityThread.java:123)
03-08 13:35:51.068: E/AndroidRuntime(623): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
03-08 13:35:51.068: E/AndroidRuntime(623): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 13:35:51.068: E/AndroidRuntime(623): at android.os.Looper.loop(Looper.java:137)
03-08 13:35:51.068: E/AndroidRuntime(623): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-08 13:35:51.068: E/AndroidRuntime(623): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 13:35:51.068: E/AndroidRuntime(623): at java.lang.reflect.Method.invoke(Method.java:511)
03-08 13:35:51.068: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-08 13:35:51.068: E/AndroidRuntime(623): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-08 13:35:51.068: E/AndroidRuntime(623): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
NPE是因为 mCalculation 变量为null。
在上面的代码中,您已声明它但未初始化它,因此在访问mCalculation.getResult()时会出现错误。
像这样初始化它,
mCalculation = new Calculation(); //if you dont have any other constructors created