如果是else,意图不起作用

时间:2016-04-25 07:28:55

标签: java android android-intent

以下是我的示例代码:

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);
}

第一个如果工作正常但第二个不是。第二阶段没有活动变化。请在这里给我一点想法。

5 个答案:

答案 0 :(得分: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.

答案 1 :(得分:0)

所以尝试在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

                }

希望这对你有用....

EDITED ::

如果字符串有效,那么

 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

                }

答案 2 :(得分:0)

假设imageUpload是一个异步任务实例,您忘记测试PENDING Status

所以最终结果看起来像这样:

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. 

理想情况下,最好为状态字符串使用静态最终变量,因为这些变量永远不会改变,这样你就不必记住它们的值意味着什么。

此外,如果您真正启动相同的Activity,无论发生什么,那么这是重复的代码,您可以在switch语句之外执行该部分。

显示Toast也是如此。在我看来,只有Toast的内容发生了变化,所以只有那些内容需要在交换机中。

答案 3 :(得分:0)

这行代码对我有用。  我们不需要为if else创建两个意图。只需创建一个并在if else之后调用startactivity。 这就对了。

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 :(得分:-1)

通过注销来检查 checkin 的值。

如果 checkin 变量的值不等于“0”,则library(splitstackshape) cSplit(x, 'x', ',', 'long')[setDT(y), on='x'][,.(x=paste(description, collapse=',')), s.no] # s.no x #1: 1 a,b,c #2: 2 b,d #3: 3 c 部分将无法执行。

尝试检查如下:

else if

另请尝试使用String checkin = imageUpload.getStatus(); Log.i("Value of checkin : >>>>>>>>",""+checkin); 代替YourActivity.this