我有三项活动A,B和C
在我的B活动中,我有一个imageview,当我从活动A到B获得一个String时,imageview应该是可见的。
当我从活动C到B获得一个字符串时,不应该看到imageview。
//From activity A
Intent iin= getIntent();
Bundle b = iin.getExtras();
//From Activity C
Intent i2=getIntent();
Bundle abcd=i2.getExtras();
if(b!=null)
{
String j =(String) b.get("arrowvisi");
Toast.makeText(getApplicationContext(), j, Toast.LENGTH_LONG).show();
if(j==b.get("arrowvisi"))
{
img_back.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Operational arrow visible", Toast.LENGTH_LONG).show();
}
else
{
if(abcd!=null)
{
String jst =(String) abcd.get("arrow_val");
if(jst==abcd.get("arrow_val"))
{
img_back.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "scan dispatch visble", Toast.LENGTH_LONG).show();
}
else
{
//img_back.setVisibility(View.GONE);
System.out.println("from scan dispatch");
Toast.makeText(getApplicationContext(), "scan dispatch not visible", Toast.LENGTH_LONG).show();
}
}
Toast.makeText(getApplicationContext(), "Operational not visible", Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
首先,要比较java中的字符串,您需要使用.equals
所以改变
if(j==b.get("arrowvisi"))
到
if(j.equals(b.getString("arrowvisi")))
和
if(jst==abcd.get("arrow_val"))
到
if(jst.equals(abcd.getString("arrow_val")))
第二,你的情况总是如此,因为你总是比较两个相等的值,即。首先获取jst
中的值,然后比较相同的值。
编辑:解决空指针更改
String j =(String) b.get("arrowvisi");
到
String j = b.getString("arrowvisi");
和
String jst =(String) abcd.get("arrow_val");
到
String jst =abcd.getString("arrow_val");
发布logcat异常。
答案 1 :(得分:0)
替换
String j = (String) b.get("arrowvisi");
带
String j = b.getString("arrowvisi");
和
String jst = (String) abcd.get("arrow_val");
带
String jst = abcd.getString("arrow_val");
字符串比较也应该像
j.equals(b.getString("arrowvisi")) // change again
和
jst.equals(abcd.getString("arrow_val")) // change again
所以,最终的ans应该是
//From activity A
Intent iin = getIntent();
Bundle b = iin.getExtras();
//From Activity C
Intent i2 = getIntent();
Bundle abcd = i2.getExtras();
if(b != null){
String j = "String to check"; // Replace content inside "" with your string
Toast.makeText(getApplicationContext(), j, Toast.LENGTH_LONG).show();
if(j.equals(b.getString("arrowvisi"))){
img_back.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Operational arrow visible", Toast.LENGTH_LONG).show();
}else{
if(abcd != null){
String jst = "String to check"; // Replace with string you want to check
if(jst.equals(abcd.getString("arrow_val"))){
img_back.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "scan dispatch visble", Toast.LENGTH_LONG).show();
}else{
//img_back.setVisibility(View.GONE);
System.out.println("from scan dispatch");
Toast.makeText(getApplicationContext(), "scan dispatch not visible", Toast.LENGTH_LONG).show();
}
}
Toast.makeText(getApplicationContext(), "Operational not visible", Toast.LENGTH_LONG).show();
}
}
同样注意,执行类似
的操作String jst = abcd.getString("arrow_val"));
if(jst.equals(abcd.getString("arrow_val"))) // this will be always true
这将导致if
语句始终为真。
2个选项:
1)删除if语句因为总是如此。
2)更改字符串jst = "Some string to compare"
(更改此内容),然后在if循环中使用jst
将getString("arrow_val")
与jst.equals(abcd.getString("arrow_val"))
进行比较
答案 2 :(得分:0)
更改此
String j = (String) b.get("arrowvisi");
与
String j = b.getString("arrowvisi");
和
String jst = (String) abcd.get("arrow_val");
与
String jst = abcd.getString("arrow_val");
也把它放在括号()
中b.get("arrowvisi")
abcd.get("arrow_val")