动态添加一个 int 到 textView
Dynamically adding an int to a textView
这似乎是一个愚蠢的问题,但出于某种原因我无法弄清楚 - 我有几个 textViews 我从对象添加文本所有字符串字段都添加得很好。但是当我尝试向 textView 添加一个 int 时,它使我的应用程序崩溃。
这是我的代码
firstLineOfAddress.setText(addline);
town.setText(town2);
county.setText(county2);
postCode.setText(post);
// the three strings up above work fine if i comment the three below out
// ask , current, done are all ints
askingPrice.setText(ask);
currentOffer.setText(current);
doneUpValue.setText(done);
这样设置:
textView.setText(10+"");
在您的代码中替换为:
askingPrice.setText(ask);
currentOffer.setText(current);
doneUpValue.setText(done);
与
askingPrice.setText(ask+"");
currentOffer.setText(current+"");
doneUpValue.setText(done+"");
问题是您必须将您的 int 转换为 String。要将您的 int 转换为 String,请使用:
Integer.toString(myInt)
或
String.valueOf(myint)
所以会是askingPrice.setText(Integer.toString(myInt))
尝试将其转换为 String
也许。
askingPrice.setText(String.format("%d",ask));
您必须将文本值设置为字符串。如此转换:
askingPrice.setText(String.valueOf(ask));
currentOffer.setText(String.valueOf(current));
doneUpValue.setText(String.valueOf(done));
当您添加任何整数时调用此...
textview.setText(""+value);
或
textview.setText(String.valueOf(value));
其中 value
是一个整数。
很好地尝试上面这样的场景
int text=0;
TextView tv = (TextView)findViewById(R.id.test);
tv.setText(text);
抛出异常
(26955): java.lang.RuntimeException: 无法启动 activity ComponentInfo{com.example.highactivity/com.example.highactivity.MainActivity}: android.content.res.Resources NotFoundException: 字符串资源 ID # 0x0
因为在设置文本中传递整数时它需要作为资源 id.At 运行 时间搜索资源 ID =0 但不会有任何资源 ID 这样它抛出异常将导致应用程序crashes.convert int to string 如果需要显示整数
这似乎是一个愚蠢的问题,但出于某种原因我无法弄清楚 - 我有几个 textViews 我从对象添加文本所有字符串字段都添加得很好。但是当我尝试向 textView 添加一个 int 时,它使我的应用程序崩溃。
这是我的代码
firstLineOfAddress.setText(addline);
town.setText(town2);
county.setText(county2);
postCode.setText(post);
// the three strings up above work fine if i comment the three below out
// ask , current, done are all ints
askingPrice.setText(ask);
currentOffer.setText(current);
doneUpValue.setText(done);
这样设置:
textView.setText(10+"");
在您的代码中替换为:
askingPrice.setText(ask);
currentOffer.setText(current);
doneUpValue.setText(done);
与
askingPrice.setText(ask+"");
currentOffer.setText(current+"");
doneUpValue.setText(done+"");
问题是您必须将您的 int 转换为 String。要将您的 int 转换为 String,请使用:
Integer.toString(myInt)
或
String.valueOf(myint)
所以会是askingPrice.setText(Integer.toString(myInt))
尝试将其转换为 String
也许。
askingPrice.setText(String.format("%d",ask));
您必须将文本值设置为字符串。如此转换:
askingPrice.setText(String.valueOf(ask));
currentOffer.setText(String.valueOf(current));
doneUpValue.setText(String.valueOf(done));
当您添加任何整数时调用此...
textview.setText(""+value);
或
textview.setText(String.valueOf(value));
其中 value
是一个整数。
很好地尝试上面这样的场景
int text=0;
TextView tv = (TextView)findViewById(R.id.test);
tv.setText(text);
抛出异常 (26955): java.lang.RuntimeException: 无法启动 activity ComponentInfo{com.example.highactivity/com.example.highactivity.MainActivity}: android.content.res.Resources NotFoundException: 字符串资源 ID # 0x0
因为在设置文本中传递整数时它需要作为资源 id.At 运行 时间搜索资源 ID =0 但不会有任何资源 ID 这样它抛出异常将导致应用程序crashes.convert int to string 如果需要显示整数