我正在尝试获取总金额并将其显示为Toast,但我收到如下错误...我已使用Long.parseLong()将字符串转换为Long但仍然没有运气。
Error:(247, 18) error: no suitable method found for makeText(Context,long,int)
method Toast.makeText(Context,CharSequence,int) is not applicable
(argument mismatch; long cannot be converted to CharSequence)
method Toast.makeText(Context,int,int) is not applicable
(argument mismatch; possible lossy conversion from long to int)
代码
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
int button = data.getIntExtra("k1", 0);
long a=0,long c1=0;
long as=0,long as1=0,long as2=0;
long bs=0,bs1=0,bs2=0;
if (button == 1) {
switch (requestCode) {
case 0:
String result = data.getStringExtra("text"); //Assume it holds 35
String b = data.getStringExtra("a");
as=Long.parseLong(result);
c.setText(" " + b + "------" + "RM " + result);
break;
case 1:
String result1 = data.getStringExtra("text");
String b1 = data.getStringExtra("a");
as1=Long.parseLong(result1);
c.setText(" " + b1 + "------" + "RM " + result1);
break;
case 2:
String result2 = data.getStringExtra("text");
String b2 = data.getStringExtra("a");
as2=Long.parseLong(result2);
c.setText(" " + b2 + "------" + "RM " + result2);
break;
}
}
else if(button==2)
{
switch (requestCode) {
case 0:
String result = data.getStringExtra("text");
String b = data.getStringExtra("a");
bs=Long.parseLong(result);
d.setText(" " + b + "------" + "RM " + result);
break;
case 1:
String result1 = data.getStringExtra("text");
String b1 = data.getStringExtra("a");
bs1=Long.parseLong(result1);
d.setText(" " + b1 + "------" + "RM " + result1);
break;
case 2:
String result2 = data.getStringExtra("text"); //Assume it holds 40
String b2 = data.getStringExtra("a");
bs2=Long.parseLong(result2);
d.setText(" " + b2 + "------" + "RM " + result2);
break;
}
long x=as+as1+as2;
long y=bs+bs1+bs2;
long amount=x+y;
Toast.makeText(getActivity().getApplicationContext(), amount, Toast.LENGTH_LONG).show();
}
它有另一种方法来写这个,因为我只需要从每个按钮获得1个值。我之间的金额是不正确的。
很抱歉,如果它是一个愚蠢的问题....
答案 0 :(得分:3)
试试这个:
Toast.makeText(getActivity().getApplicationContext(), String.valueOf(amount), Toast.LENGTH_LONG).show();