java - 切换条件在 Android 中不起作用

标签 java android if-statement switch-statement

所以我有这个代码:

package com.sunil.phpconnect;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
        Button nao;
        Button foto;
        Button novamen;
        TextView ola;    
        HttpClient httpclient1, httpclient;
        HttpGet request1, request;
        HttpResponse response1, response;
        String url,iduser, urlmensagens;


             @Override
             public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              Bundle  extras = getIntent().getExtras();
              iduser= extras.getString("userid");


               TextView result = (TextView) findViewById(R.id.tvResult);
               //String g = result.getText().toString();
              // String h = "valor";

               urlmensagens = ("http://mywebsite.php?iduser="+iduser);
               novamen = (Button) findViewById(R.id.mensagens);


                   //cenas da net
                    try {
                     httpclient1 = new DefaultHttpClient();
                     request1 = new HttpGet(urlmensagens);
                     response1 = httpclient1.execute(request1);

                    }
                    catch (Exception e){

                    }
                    try{ 
                      BufferedReader dr = new BufferedReader(new InputStreamReader(
                        response1.getEntity().getContent()));


                      String mensage = "";


                      mensage = dr.readLine();

                      String check_sms = mensage;

                      Toast.makeText(getApplicationContext(), check_sms,
                              Toast.LENGTH_LONG).show();



                //novamen.setText(check_sms + " Mensagens por ler!");





                      switch(check_sms) {
                      case "b":
                          novamen.setVisibility(View.GONE);
                          break;
                      case "a":
                          novamen.setVisibility(View.VISIBLE);
                          break;
                      default:
                          novamen.setVisibility(View.GONE);
                  }


                    } catch (Exception e) {
                          // Code to handle exception  

                         }

              url = ("http://mywebsite.php?action=requestuserdata&userid="+iduser);


              try {
               httpclient = new DefaultHttpClient();
               request = new HttpGet(url);
               response = httpclient.execute(request);
              }

              catch (Exception e) {
               }


              try {
               BufferedReader rd = new BufferedReader(new InputStreamReader(
                 response.getEntity().getContent()));
               String line = "";
               line = rd.readLine();



                   if(line == null){
                       Toast.makeText(getApplicationContext(), "Numero nao atribuido.",
                                  Toast.LENGTH_LONG).show();
                       Intent wowmuch = new Intent(getApplicationContext(), pin.class);
                     startActivity(wowmuch);    
                   }else{
                       result.append(line);

                   }

              } catch (Exception e) {

              }

              novamen = (Button) findViewById(R.id.mensagens);
                novamen.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                            chamaMensagens();
                            }

                        public void chamaMensagens () {

                            Intent mens = new Intent(getApplicationContext(),mensagens.class);
                            mens.putExtra("userid", iduser);
                            startActivity(mens);}});


              foto = (Button) findViewById(R.id.button1);
              foto.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    Chamafoto();
                }
                public void Chamafoto() {

                    Intent wowmuch = new Intent(getApplicationContext(), Foto.class);
                    wowmuch.putExtra("userid", iduser);        
                    startActivity(wowmuch);
                }
            });
              Toast.makeText(getApplicationContext(), iduser, 
                      Toast.LENGTH_SHORT).show();
              nao = (Button) findViewById(R.id.button2);
                nao.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                            chamaConsulta();
                            }
                    }
                );
                }

                        public void chamaConsulta () {

                            Intent wowmuch = new Intent(getApplicationContext(),pin.class);

                            startActivity(wowmuch); 
                        }


}

当我到达这一部分时:

switch(check_sms) {
                          case "b":
                              novamen.setVisibility(View.GONE);
                              break;
                          case "a":
                              novamen.setVisibility(View.VISIBLE);
                              break;
                          default:
                              novamen.setVisibility(View.GONE);
                      }

当变量“check_sms”值为“b”时,该按钮假定为不可见/消失。

我测试了变量是否显示并且它位于 Toast 中。

我也尝试过 if 语句,但什么也没做。

最佳答案

switch(someInteger) 可以工作。
switch(someChar) 可以工作。
switch(someEnum) 可以工作。
switch(someString) 仅适用于 JAVA 1.7

据我所知,Android 尚不支持 Java 1.7。

关于java - 切换条件在 Android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23802043/

相关文章:

python - For Loop with If 语句只返回第一个元素

c++ - 如何 "do, if expression increased"?

java - 获取数组第二维的长度

javascript - 在 Android 上的 Chrome 上的 jQuery.ready() 中没有自动播放 mp3 文件

java - Android 查看资源膨胀

android - Kotlin 协程返回字符串

java - 第一次点击按钮后,按钮停止工作

java - Android:在应该向后兼容的 3.0 应用程序上崩溃

java - 如何在按下按钮之前激活按钮?

java - 突然关闭应用程序而不进行任何 Activity ,抛出 IllegalStateException onMeasure()