2013-01-25 50 views
2

我有兩個類,一個叫做SQLiteExample,它有一個switch語句。在這個開關中是一個按鈕點擊監聽器,我想要將生成的int讀入TextView,這不起作用。我可以將int讀入Toast消息並無誤顯示。然而,當閱讀到一個textview我不知道爲什麼它會在logcat中出現錯誤,當我這樣做。 TextView已正確註冊。setText方法的EditText或TextView不工作在switch語句中

public class SQLiteExample extends Activity implements OnClickListener { 

case R.id.bSQLnumberofrows: 
     PlayGame ec = new PlayGame(this); 
     ec.open(); 
     int x = ec.fetchPlacesCount(); 
     ec.close(); 
     showNumbers.setText(x); // setText not working 
        // Toast message works perfectly and displays value of "x" on screen 
     Toast.makeText(SQLiteExample.this, "value of x " + x , Toast.LENGTH_SHORT).show(); 
     break; 

這裏是logcat的錯誤:

01-25 10:52:04.540: E/AndroidRuntime(8449): FATAL EXCEPTION: main 
01-25 10:52:04.540: E/AndroidRuntime(8449): android.content.res.Resources$NotFoundException: String resource ID #0x5 
01-25 10:52:04.540: E/AndroidRuntime(8449):  at android.content.res.Resources.getText(Resources.java:247) 
01-25 10:52:04.540: E/AndroidRuntime(8449):  at android.widget.TextView.setText(TextView.java:3473) 
01-25 10:52:04.540: E/AndroidRuntime(8449):  at com.example.dbsample.SQLiteExample.onClick(SQLiteExample.java:127) 

的線127是其中的例外是:showNumbers.setText(X);

和其他類瑣事,有一個方法來獲得的行數在SQLite數據庫作爲一個int:

public class PlayGame { 

public int fetchPlacesCount() { 
     int count = 0; 
     Cursor q = ourDatabase.rawQuery("SELECT COUNT(*) from " + DATABASE_TABLE, null); 
     q.moveToFirst(); 
     count = q.getInt(0); 
     return count; 
    } 

回答

3

要調用的setText()的版本,它接受一個int參數預計引用XML文件中的字符串資源。您需要將int轉換爲String才能在TextView中顯示其值。請注意,使用烤麪包時,您正在使用"value of x " + x隱式執行此操作。您可以通過調用String.valueOf(x)int明確轉換爲String

+0

這間接地解決了我在4小時搜索後沒有意識到的另一個問題。謝謝! – runfaj