你好我有一个活动,应该只计算每个生命周期的变化,所以我这样开发但是当方向改变时它显示异常:
logcat的
01-23 22:04:45.075: I/Lab-ActivityOne(7963): Entered the onCreate() method
01-23 22:04:45.085: I/Lab-ActivityOne(7963): Entered the onStart() method
01-23 22:04:45.085: I/Lab-ActivityOne(7963): Entered the onResume() method
01-23 22:04:45.185: D/libEGL(7963): loaded /system/lib/egl/libGLES_android.so
01-23 22:04:45.215: D/libEGL(7963): loaded /system/lib/egl/libEGL_adreno200.so
01-23 22:04:45.235: D/libEGL(7963): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
01-23 22:04:45.235: D/libEGL(7963): loaded /system/lib/egl/libGLESv2_adreno200.so
01-23 22:04:45.395: I/Adreno200-EGLSUB(7963): <ConfigWindowMatch:1991>: Format RGBA_8888.
01-23 22:04:45.846: D/OpenGLRenderer(7963): Enabling debug mode 0
01-23 22:04:45.986: D/OpenGLRenderer(7963): has fontRender patch
01-23 22:04:46.046: D/OpenGLRenderer(7963): has fontRender patch
01-23 22:04:54.815: D/OpenGLRenderer(7963): Flushing caches (mode 0)
01-23 22:04:54.825: I/Lab-ActivityOne(7963): Entered the onPause() method
01-23 22:04:54.825: I/Lab-ActivityOne(7963): YESSSSSSSSSSSS--1
01-23 22:04:54.825: I/Lab-ActivityOne(7963): Entered the onStop() method
01-23 22:04:54.825: I/Lab-ActivityOne(7963): Entered the onDestroy() method
01-23 22:04:54.835: D/memalloc(7963): /dev/pmem: Unmapping buffer base:0x5217b000 size:4608000 offset:3072000
01-23 22:04:54.835: D/memalloc(7963): /dev/pmem: Unmapping buffer base:0x52cb2000 size:7557120 offset:6021120
01-23 22:04:54.985: I/Lab-ActivityOne(7963): ---null
01-23 22:04:54.985: D/AndroidRuntime(7963): Shutting down VM
01-23 22:04:54.995: E/AndroidRuntime(7963): Caused by: java.lang.NullPointerException
01-23 22:04:54.995: E/AndroidRuntime(7963): at course.labs.activitylab.ActivityOne.onCreate(ActivityOne.java:126)
01-23 22:04:54.995: E/AndroidRuntime(7963): at android.app.Activity.performCreate(Activity.java:4538)
01-23 22:04:54.995: E/AndroidRuntime(7963): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
01-23 22:04:54.995: E/AndroidRuntime(7963): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
01-23 22:04:54.995: E/AndroidRuntime(7963): ... 12 more
我的主要活动:
public class ActivityOne extends Activity {
private static final String RESTART_KEY = "restart";
private static final String RESUME_KEY = "resume";
private static final String START_KEY = "start";
private static final String CREATE_KEY = "create";
// String for LogCat documentation
private final static String TAG = "Lab-ActivityOne";
Hashtable count_info = new Hashtable();
int mCreate=0;
int mRestart=0;
int mStart=0;
int mResume=0;
TextView mTvCreate;
TextView mTvRestart;
TextView mTvStart;
TextView mTvResume;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
// TODO: Assign the appropriate TextViews to the TextView variables
// Hint: Access the TextView by calling Activity's findViewById()
// textView1 = (TextView) findViewById(R.id.textView1);
mTvCreate = (TextView) findViewById(R.id.create);
mTvRestart = (TextView) findViewById(R.id.restart);
mTvStart = (TextView) findViewById(R.id.start);
mTvResume = (TextView) findViewById(R.id.resume);
Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo);
launchActivityTwoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO:
// Launch Activity Two
// Hint: use Context's startActivity() method
// Create an intent stating which Activity you would like to start
// Launch the Activity using the intent
Intent activity_two = new Intent(getApplicationContext(), ActivityTwo.class);
startActivity(activity_two);
}
});
// Check for previously saved state
if (savedInstanceState != null) {
// TODO:
// Restore value of counters from saved state
// Only need 4 lines of code, one for every count variable
Log.i(TAG,"---"+count_info.get("onCreate"));
mCreate = (Integer) count_info.get("onCreate");
mStart = (Integer) count_info.get("onStart");
mResume = (Integer) count_info.get("onResume");
mRestart = (Integer) count_info.get("onRestart");
}
// TODO: Emit LogCat message
Log.i(TAG,"Entered the onCreate() method");
// TODO:
// Update the appropriate count variable
mCreate++;
// Update the user interface via the displayCounts() method
displayCounts();
}
// Lifecycle callback overrides
@Override
public void onStart() {
super.onStart();
// TODO: Emit LogCat message
Log.i(TAG,"Entered the onStart() method");
// TODO:
// Update the appropriate count variable
mStart++;
// Update the user interface
displayCounts();
}
@Override
public void onResume() {
super.onResume();
// TODO: Emit LogCat message
Log.i(TAG,"Entered the onResume() method");
// TODO:
// Update the appropriate count variable
mResume++;
// Update the user interface
displayCounts();
}
@Override
public void onPause() {
super.onPause();
// TODO: Emit LogCat message
Log.i(TAG,"Entered the onPause() method");
}
@Override
public void onStop() {
super.onStop();
// TODO: Emit LogCat message
Log.i(TAG,"Entered the onStop() method");
}
@Override
public void onRestart() {
super.onRestart();
// TODO: Emit LogCat message
Log.i(TAG,"Entered the onRestart() method");
// TODO:
// Update the appropriate count variable
mRestart++;
// Update the user interface
displayCounts();
}
@Override
public void onDestroy() {
super.onDestroy();
// TODO: Emit LogCat message
Log.i(TAG,"Entered the onDestroy() method");
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// TODO:
// Save state information with a collection of key-value pairs
// 4 lines of code, one for every count variable
count_info.put("onCreate", mCreate);
count_info.put("onStart", mStart);
count_info.put("onResume", mResume);
count_info.put("onRestart", mRestart);
Log.i(TAG,"YESSSSSSSSSSSS--"+count_info.get("onCreate"));
}
// Updates the displayed counters
public void displayCounts() {
mTvCreate.setText("onCreate() calls: " + mCreate);
mTvStart.setText("onStart() calls: " + mStart);
mTvResume.setText("onResume() calls: " + mResume);
mTvRestart.setText("onRestart() calls: " + mRestart);
}
}
我的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/onCreate" />
<TextView android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/onStart" />
<TextView android:id="@+id/resume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/onResume" />
<TextView android:id="@+id/restart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/onRestart" />
<Button android:id="@+id/bLaunchActivityTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button" />
</LinearLayout>`
答案 0 :(得分:2)
这样做:
static Hashtable count_info = new Hashtable();
答案 1 :(得分:2)
把这个:
savedInstanceState.putSerializable("KEY", count_info);
并在你的创建中:
if (savedInstanceState != null) {
count_info = (Hashtable) savedInstanceState.getSerializable("KEY");
//initialize variables here
}
else
count_info = new Hashtable();