Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android EditText或TextView的setText方法在switch语句内部不起作用_Android_Textview_Android Edittext_Settext - Fatal编程技术网

Android EditText或TextView的setText方法在switch语句内部不起作用

Android EditText或TextView的setText方法在switch语句内部不起作用,android,textview,android-edittext,settext,Android,Textview,Android Edittext,Settext,我有两个类,一个叫做SQLiteExample,它有一个switch语句。在这个开关中有一个按钮点击监听器,我想在这里将结果int读入TextView,但这不起作用。我可以将int读入Toast消息并显示它,没有错误。然而,当我把它读入文本视图时,我不知道为什么我这样做时,它会在logcat中出错。TextView已正确注册 public class SQLiteExample extends Activity implements OnClickListener { case R.id.

我有两个类,一个叫做SQLiteExample,它有一个switch语句。在这个开关中有一个按钮点击监听器,我想在这里将结果int读入TextView,但这不起作用。我可以将int读入Toast消息并显示它,没有错误。然而,当我把它读入文本视图时,我不知道为什么我这样做时,它会在logcat中出错。TextView已正确注册

 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行是例外情况:showNumber.setText(x)

另一个类是PlayGame,它有一个方法来获取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;
    }

您正在调用
setText()
的版本,该版本接受一个
int
参数,该参数应引用XML文件中的字符串资源。您需要将
int
转换为
字符串
,以便在文本视图中显示其值。请注意,在使用Toast时,您是使用
“x的值”+x
隐式执行此操作的。通过调用
String.valueOf(x)
int
可以显式地将
int
转换为
String
,这间接地解决了我在搜索4小时后没有意识到的另一个问题。非常感谢。