2012-01-09 56 views
0
public class DataHelper extends Activity{ 

    TextView tvRslt; 

    public static int insert(String firstFB, String firstRL) { 
     // TODO Auto-generated method stub 
     Integer a = new Integer(firstFB).intValue(); 
     Integer b = new Integer(firstRL).intValue(); 
     int firstResult = a/b; 
     } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.result); 

我想設置整數「firstResult」以顯示在TextEdit「tvRslt」上。將TextView設置爲整數的值

我該怎麼做?

如果需要更多信息,請告訴我。

+0

好的,這裏有太多的想法可以提供任何體面的答案。首先,變量firstResult在插入方法的最後一行中聲明,不是一個類變量,那麼你打算如何設置任何值到它的值?還有什麼是TextView對象和Bundle對象的源代碼? firstResult聲明也應該包裝在try/catch塊中,以除以零例外。 – 2012-01-09 18:20:30

+0

tvRslt.setText(firstResult); – 2012-01-09 18:22:27

+0

那好吧,我該如何將它設置爲一個類變量,以及如何設置異常嗎? – MiKenning 2012-01-09 18:24:25

回答

0

隨着異常處理:

try { 
    tvRslt.setText(firstResult + ""); 
} catch (DivideByZeroException e) { 
    // Do something with exception condition. 
} 

無異常處理:

// Before dividing check if denominator is 0. 
int firstResult = (b == 0) ? 0 : a/b; 
tvRslt.setText(firstResult + ""); 

這並不當然需要你有TextView的一個參考,因爲我認爲你對你所做的代碼不向我們展示。