出于某种原因,当我在按钮监听器中调用startActivityForResult
并从活动返回时,所有按钮此后都停止工作。它通过并退出onActivityResult
就好了,应用程序没有冻结,因为我仍然可以点击按钮,尽管他们什么都不做。启动应用程序时它可以正常工作。我在这里缺少什么?
public class MainActivity extends Activity {
static final int CAMERA_RESULT_CODE = 1;
static final int INFO_RESULT_CODE = 2;
static final int SYMPTOM_RESULT_CODE = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// Set the user interface elements
final Button btnCameraActivity = (Button) findViewById(R.id.btnCameraActivity);
btnCameraActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, CropActivity.class);
startActivityForResult(intent, CAMERA_RESULT_CODE);
}
});
final Button btnAgeActivity = (Button) findViewById(R.id.btnAgeActivity);
btnAgeActivity.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AgeActivity.class);
startActivityForResult(intent, INFO_RESULT_CODE);
}
});
final Button btnSymptomActivity = (Button) findViewById(R.id.btnSymptomActivity);
btnSymptomActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SymptomActivity.class);
startActivityForResult(intent, SYMPTOM_RESULT_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
setContentView(R.layout.activity_main);
switch (requestCode) {
case CAMERA_RESULT_CODE:
if (resultCode == RESULT_OK) {
final CheckBox cb1 = (CheckBox) findViewById(R.id.cbCameraActivity);
cb1.setChecked(true);
}
break;
case INFO_RESULT_CODE:
if (resultCode == RESULT_OK) {
final CheckBox cb2 = (CheckBox) findViewById(R.id.cbAgeActivity);
cb2.setChecked(true);
}
break;
case SYMPTOM_RESULT_CODE:
if (resultCode == RESULT_OK) {
final CheckBox cb3 = (CheckBox) findViewById(R.id.cbSymptomActivity);
cb3.setChecked(true);
}
break;
}
}
}
答案 0 :(得分:3)
setContentView()
中的onActivityResult()
来电正在替换您在onCreate()
中设置的布局,因此您设置Button
的{{1}}个实例已经不见了。
只需从OnClickListener
的顶部删除该来电。
答案 1 :(得分:0)
将此代码放在onResume
方法
final Button btnCameraActivity = (Button) findViewById(R.id.btnCameraActivity);
btnCameraActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, CropActivity.class);
startActivityForResult(intent, CAMERA_RESULT_CODE);
}
});
final Button btnAgeActivity = (Button) findViewById(R.id.btnAgeActivity);
btnAgeActivity.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AgeActivity.class);
startActivityForResult(intent, INFO_RESULT_CODE);
}
});
final Button btnSymptomActivity = (Button) findViewById(R.id.btnSymptomActivity);
btnSymptomActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SymptomActivity.class);
startActivityForResult(intent, SYMPTOM_RESULT_CODE);
}
});
此类代码通常可以通过将您的课程扩展到View.OnClickListener