[英]OnClickListener stops working after returning from another activity
For some reason when I call startActivityForResult
within a button listener, and return from the activity, all the buttons stop working thereafter. 由于某些原因,当我在按钮侦听器中调用
startActivityForResult
并从活动中返回时,此后所有按钮都停止工作。 It goes through and exits onActivityResult
just fine, and the app doesn't freeze since I can still click on the buttons although they don't do anything. 它会通过并退出
onActivityResult
很好,并且该应用程序不会冻结,因为尽管它们没有执行任何操作,但我仍然可以单击按钮。 When starting the app it works as it should. 启动应用程序时,它可以正常工作。 What am I missing here?
我在这里想念什么?
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;
}
}
}
The setContentView()
call you have in onActivityResult()
is replacing the layout you've setup in onCreate()
, so those Button
instances that you set the OnClickListener
s on are gone. 您在
onActivityResult()
的setContentView()
调用将替换您在onCreate()
设置的布局,因此设置了OnClickListener
的那些Button
实例都消失了。
Simply remove that call from the top of onActivityResult()
. 只需从
onActivityResult()
的顶部删除该调用即可。
put this code in onResume
method 将此代码放在
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);
}
});
also this code can be generally simplified by extending youre class to View.OnClickListener
也可以通过将您的类扩展到
View.OnClickListener
来简化此代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.