[英]putextra and getextra not passing string to next activity
I have three activities A,B and C 我有三个活动A,B和C
In my B activity I have one imageview, when I get a String from activity A to B, the imageview should be visible. 在我的B活动中,我有一个imageview,当我从活动A到B获得一个String时,该imageview应该可见。
When I get a String from activity C to B then the imageview should not be visible. 当我从活动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();
}
}
First of all,to compare strings in java,you need to use .equals
首先,要比较java中的字符串,需要使用
.equals
so change 所以改变
if(j==b.get("arrowvisi"))
to 至
if(j.equals(b.getString("arrowvisi")))
and 和
if(jst==abcd.get("arrow_val"))
to 至
if(jst.equals(abcd.getString("arrow_val")))
And second,your condition always will be true,because you are comparing two equal values always,ie first getting value in jst
and then comparing same value. 其次,您的条件将永远为真,因为您总是在比较两个相等的值,即首先在
jst
获取值,然后再比较相同的值。
Edit : to resolve null pointer change 编辑:解决空指针更改
String j =(String) b.get("arrowvisi");
to 至
String j = b.getString("arrowvisi");
and 和
String jst =(String) abcd.get("arrow_val");
to 至
String jst =abcd.getString("arrow_val");
And post logcat exception. 并发布logcat异常。
Replace 更换
String j = (String) b.get("arrowvisi");
with 与
String j = b.getString("arrowvisi");
and 和
String jst = (String) abcd.get("arrow_val");
with 与
String jst = abcd.getString("arrow_val");
also string comparison should be like 还字符串比较应该像
j.equals(b.getString("arrowvisi")) // change again
and 和
jst.equals(abcd.getString("arrow_val")) // change again
So, the final ans should be something like 因此,最终的答案应该类似于
//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();
}
}
Also note that, doing something like 还要注意 ,做类似的事情
String jst = abcd.getString("arrow_val"));
if(jst.equals(abcd.getString("arrow_val"))) // this will be always true
this, will result in if
statement being always true. 这将导致
if
语句始终为true。
2 options : 2种选择:
1) Remove if statement because always true. 1)删除if语句,因为它始终为true。
2) change String jst = "Some string to compare"
(change this) and now compare jst
with getString("arrow_val")
using jst.equals(abcd.getString("arrow_val"))
in your if loop 2)更改String
jst = "Some string to compare"
(对此jst = "Some string to compare"
更改),现在在if循环中使用jst.equals(abcd.getString("arrow_val"))
比较jst
与getString("arrow_val")
change this 改变这个
String j = (String) b.get("arrowvisi");
with 与
String j = b.getString("arrowvisi");
and 和
String jst = (String) abcd.get("arrow_val");
with 与
String jst = abcd.getString("arrow_val");
also put this in bracket() 也把它放在括号中
b.get("arrowvisi")
abcd.get("arrow_val")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.