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;
}
這間接地解決了我在4小時搜索後沒有意識到的另一個問題。謝謝! – runfaj