计算总金额
Calculate the total amount
我正在尝试获取总量并将其显示为 Toast,但出现如下错误...我已使用 Long.parseLong() 将 String 转换为 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 个值。之间,我得到的金额不正确。
抱歉,如果这是一个愚蠢的问题....
试试这个:
Toast.makeText(getActivity().getApplicationContext(), String.valueOf(amount), Toast.LENGTH_LONG).show();
我正在尝试获取总量并将其显示为 Toast,但出现如下错误...我已使用 Long.parseLong() 将 String 转换为 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 个值。之间,我得到的金额不正确。
抱歉,如果这是一个愚蠢的问题....
试试这个:
Toast.makeText(getActivity().getApplicationContext(), String.valueOf(amount), Toast.LENGTH_LONG).show();