問題:- startActivity() が Android system_processによって無限に呼び出されます。以下のコードは MainActivity を開きますが、MainActivityがフリーズ/ハングし、数分後に Android がシャットダウンします。
2 つのアクティビティがあります。
- 登録活動
- 主な活動
登録プロセスが完了したら、ユーザーをホーム画面 (ieMainActivity) にリダイレクトしたいので、このコードを使用してアクティビティを開始します:--
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}
MainActivity コード:
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));
}
});
}
RegisterActivity コード:
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() が無限に呼び出されます。