[英]switch condition not working in Android
所以我有這段代碼:
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。
開啟String
?? 去Enum
...我猜你已經用jdk1.7編譯了src代碼..使用jdk1.6編譯了它...尚不支持jdk1.7 ..
只能在指定的Der Golem中使用java 1.7中的字符串。 您可以使用enum,char,int類型。 您可以參考http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html了解更多信息。
從上面的鏈接
與if-then和if-then-else語句不同,switch語句可以具有許多可能的執行路徑。 開關適用於byte,short,char和int基本數據類型。 它也適用於枚舉類型(在Enum Types中討論),String類以及一些包裝某些基本類型的特殊類:Character,Byte,Short和Integer(在Numbers和Strings中討論)。
當然,您可以在Java 7中使用swich,但是在這種情況下(只有2個選項),我將使用
if ( "a".equalsIgnoreCase( check_sms ) ){
....
} else {
....
}
切勿為此目的使用check_sms ==“ a”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.