问题:- startActivity() 被 android system_process无限次调用。所以下面的代码打开了MainActivity但随后 MainActivity 被冻结/挂起,几分钟后 android 自行关闭。
我有两个活动:
- 注册活动
- 主要活动
注册过程完成后,我想将用户重定向到主屏幕(即MainActivity),所以我使用此代码启动活动:-
Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
startActivity(intent);
}
});
但是 startActivity() 被android系统无限次调用。我通过 Logcat 发现了这一点。logcat 的输出是:--
03-20 10:08:25.756: I/ActivityManager(63): Starting activity: Intent { cmp=com.techdeedapps.fallonmoving/.MainActivity }
03-20 10:08:25.826: W/InputManagerService(63): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@44c7f1b0 (uid=10026 pid=316)
03-20 10:08:25.826: W/InputManagerService(63): Client not active, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44c4ef28
03-20 10:08:25.966: I/ActivityManager(63): Starting activity: Intent { cmp=com.techdeedapps.fallonmoving/.MainActivity }
03-20 10:08:26.036: W/InputManagerService(63): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@44c7f1b0 (uid=10026 pid=316)
03-20 10:08:26.036: W/InputManagerService(63): Client not active, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44c28e48
03-20 10:08:26.176: I/ActivityManager(63): Starting activity: Intent { cmp=com.techdeedapps.fallonmoving/.MainActivity }
03-20 10:08:26.246: W/InputManagerService(63): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@44c7f1b0 (uid=10026 pid=316)
03-20 10:08:26.246: W/InputManagerService(63): Client not active, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44c11b98
03-20 10:08:26.396: I/ActivityManager(63): Starting activity: Intent { cmp=com.techdeedapps.fallonmoving/.MainActivity }
03-20 10:08:26.437: D/dalvikvm(316): GREF has increased to 201
03-20 10:08:26.606: I/ActivityManager(63): Starting activity: Intent { cmp=com.techdeedapps.fallonmoving/.MainActivity }
03-20 10:08:26.676: W/InputManagerService(63): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@44c7f1b0 (uid=10026 pid=316)
03-20 10:08:26.676: W/InputManagerService(63): Client not active, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44de2330
03-20 10:08:26.816: I/ActivityManager(63): Starting activity: Intent { cmp=com.techdeedapps.fallonmoving/.MainActivity }
03-20 10:08:26.826: E/SurfaceFlinger(63): createSurface() failed, generateId = -12
03-20 10:08:26.826: W/WindowManager(63): OutOfResourcesException creating surface
03-20 10:08:26.826: I/WindowManager(63): Out of memory for surface! Looking for leaks...
03-20 10:08:26.826: W/WindowManager(63): No leaked surfaces; killing applicatons!
03-20 10:08:26.826: W/ActivityManager(63): Killing processes for memory at adjustment 0
03-20 10:08:26.826: W/ActivityManager(63): Killing for memory: ProcessRecord{44f32af0 316:com.techdeedapps.fallonmoving/10026} (adj 0)
03-20 10:08:26.826: I/Process(63): Sending signal. PID: 316 SIG: 9
03-20 10:08:26.836: W/WindowManager(63): Looks like we have reclaimed some memory, clearing surface for retry.
03-20 10:08:26.836: W/WindowManager(63): Due to memory failure, waiting a bit for next layout
03-20 10:08:26.856: I/ActivityManager(63): Process com.techdeedapps.fallonmoving (pid 316) has died.
03-20 10:08:27.176: I/WindowManager(63): WIN DEATH: Window{44fac7d8 com.techdeedapps.fallonmoving/com.techdeedapps.fallonmoving.MainActivity paused=false}
03-20 10:08:27.196: I/WindowManager(63): WIN DEATH: Window{44faa318 com.techdeedapps.fallonmoving/com.techdeedapps.fallonmoving.MainActivity paused=false}
主要活动代码:
public class MainActivity extends Activity {
AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
builder = new AlertDialog.Builder(this);
Button btnEnter = (Button) findViewById(R.id.btnEnter);
btnEnter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (RegisterActivity.loggedIn) {
startActivity(new Intent(MainActivity.this,
AddActivity.class));
} else {
Utility.showAlertDialog(builder, "Please Log In!");
}
}
Button btnSettings = (Button) findViewById(R.id.btnSettings);
btnSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,
RegisterActivity.class));
}
});
}
注册活动代码:
public class RegisterActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
builder = new AlertDialog.Builder(this);
etName = (TextView) findViewById(R.id.etName);
etEmail = (TextView) findViewById(R.id.etEmail);
Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnsubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(RegisterActivity.this,
MainActivity.class);
startActivity(intent);
}
});
}
}
这个 logcat 输出是连续的,直到我关闭模拟器!如果我不关闭模拟器,那么 android 会自动重启。只是系统挂起,因为它无限调用 startActivity()!