问题
Here is my sample code:
String checkin = imageUpload.getStatus();
if (checkin.equals("1")) {
Toast.makeText(FullImageActivity.this, name+" "+ "Checked In", Toast.LENGTH_LONG).show();
Intent i = new Intent(FullImageActivity.this, MainActivity.class);
startActivity(i);
//openCamera.setText("CheckOut");
} else if (checkin.equals("0")){
Toast.makeText(getApplicationContext(), name + " " + "Checked Out", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
The first one if is working fine but the second was is not. No activity change in the second stage. Please give me idea here.
回答1:
In addition to the answers here ,it seems that this issue is by multiple problems. As some here pointed out to take FullImageActivity.this instead of getApplicationContext(), my assumption was that it was not rebuild correctly. So for everybody who has the same problem: On some devices(eg. Huawei Ascend mate 7) , Android Studio doesn´t install the new version after an update correctly althought it shows this in the Run tab.
The best way to update after some code changes is to uninstall from device, clean project and reinstall your app.
So, this answer is not a code example (this part about the context is allready said) but important and fixed the problem of the questioner.
回答2:
SO try using it in Switch case :
int checkin = Integer.parseInt(imageUpload.getStatus());
switch(checkin)
{
case 1:
Toast.makeText(FullImageActivity.this, name+" "+ "Checked In", Toast.LENGTH_LONG).show();
Intent i = new Intent(FullImageActivity.this, Activiy_1.class);
startActivity(i);
//openCamera.setText("CheckOut");
break;
case 0:
Toast.makeText(FullImageActivity.this, name + " " + "Checked Out", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), Activiy_0.class);
startActivity(intent);''
break;
default:
//default methods
}
Hope this works for you....
EDITED ::
If string works then
String checkin = imageUpload.getStatus();
switch(checkin)
{
case "1":
Toast.makeText(FullImageActivity.this, name+" "+ "Checked In", Toast.LENGTH_LONG).show();
Intent i = new Intent(getActivity().this, MainActivity.class);
startActivity(i);
//openCamera.setText("CheckOut");
break;
case "0":
Toast.makeText(getApplicationContext(), name + " " + "Checked Out", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getActivity().this, MainActivity.class);
startActivity(intent);''
break;
default:
//default methods
}
回答3:
This lines of Code worked for me. We dont need to create two intents for if else. Just create one and call the startactivity after if else. That is it.
ImageUpload imageUpload = response.body();
Intent i = new Intent(FullImageActivity.this, MainActivity.class);
String checkin = imageUpload.getStatus();
if (checkin.equals("1")) {
Toast.makeText(FullImageActivity.this, name+" "+ "Checked In", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(FullImageActivity.this, name + " " + "Checked Out", Toast.LENGTH_LONG).show();
}
startActivity(i);
回答4:
Assuming imageUpload is an asynch task instance, you forgot to test for the PENDING Status.
So the final result would look something like this:
String message = "";
String checkin = imageUpload.getStatus();
switch(checkin) {
case Status.FINISHED:
message = "Checked In";
break;
case Status.RUNNING:
message = "Checked Out";
break;
case Status.PENDING:
message = "Trying";
break;
default:
message = "Arghhhh";
}
Toast.makeText(FullImageActivity.this, name + " " + message, Toast.LENGTH_LONG).show();
Intent i = new Intent(FullImageActivity.this, Activiy_1.class);
startActivity(i);
// You'll have to test this yourself. I didn't test it.
Ideally, it's better to use static final variables for the status strings, since those never change and this way you don't have to remember what their values mean.
Also if you really start the same Activity whatever happens, then that's duplicate code, and you can do that part outside of the switch statement.
The same goes for showing the Toast. It seems to me like only the content of the Toast changes, so only that content needs to be in the switch.
来源:https://stackoverflow.com/questions/36834389/intent-does-not-work-in-case-of-if-else