2015-10-28 235 views
-1

我正在嘗試獲取總金額並將其顯示爲吐司,但出現以下錯誤...我已使用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

寫'量+「」' –

+0

我得到的金額是不正確的 – Tony

回答

3

試試這個:

Toast.makeText(getActivity().getApplicationContext(), String.valueOf(amount), Toast.LENGTH_LONG).show(); 
+0

我得到的金額是不正確的 – Tony

+1

我的答案解決了您的問題,它不能建立,但不是計算問題 – PatrickMA

+0

它有另一種方式來寫這個,因爲我只需要從每個按鈕得到1值/ – Tony