我的目标是找到用户已选择的SeekBar
金额(满分100分),并在TextView
中显示此金额。这似乎是直截了当的,但由于某种原因不能用我的方法(如下所示)
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textview = (TextView) findViewById(R.id.textView);
SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
textview.setText(seekBar.getProgress());
}
}
然而,应用程序崩溃(LogCat
如下所示)。我已经确定错误来自这行代码
textview.setText(seekBar.getProgress());
为什么这行代码会导致错误?我有什么办法可以做到这一点吗?
06-15 16:22:30.166: E/AndroidRuntime(24811): FATAL EXCEPTION: main
06-15 16:22:30.166: E/AndroidRuntime(24811): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sbm/com.example.sbm.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
06-15 16:22:30.166: E/AndroidRuntime(24811): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
06-15 16:22:30.166: E/AndroidRuntime(24811): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
06-15 16:22:30.166: E/AndroidRuntime(24811): at android.app.ActivityThread.access$700(ActivityThread.java:165)
06-15 16:22:30.166: E/AndroidRuntime(24811): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
06-15 16:22:30.166: E/AndroidRuntime(24811): at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 16:22:30.166: E/AndroidRuntime(24811): at android.os.Looper.loop(Looper.java:137)
06-15 16:22:30.166: E/AndroidRuntime(24811): at android.app.ActivityThread.main(ActivityThread.java:5455)
06-15 16:22:30.166: E/AndroidRuntime(24811): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 16:22:30.166: E/AndroidRuntime(24811): at java.lang.reflect.Method.invoke(Method.java:525)
06-15 16:22:30.166: E/AndroidRuntime(24811): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
06-15 16:22:30.166: E/AndroidRuntime(24811): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
06-15 16:22:30.166: E/AndroidRuntime(24811): at dalvik.system.NativeStart.main(Native Method)
06-15 16:22:30.166: E/AndroidRuntime(24811): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
06-15 16:22:30.166: E/AndroidRuntime(24811): at android.content.res.Resources.getText(Resources.java:1068)
06-15 16:22:30.166: E/AndroidRuntime(24811): at android.widget.TextView.setText(TextView.java:4546)
06-15 16:22:30.166: E/AndroidRuntime(24811): at com.example.sbm.MainActivity.onCreate(MainActivity.java:19)
06-15 16:22:30.166: E/AndroidRuntime(24811): at android.app.Activity.performCreate(Activity.java:5372)
06-15 16:22:30.166: E/AndroidRuntime(24811): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
06-15 16:22:30.166: E/AndroidRuntime(24811): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
06-15 16:22:30.166: E/AndroidRuntime(24811): ... 11 more
答案 0 :(得分:0)
你需要
textView.setText(Integer.toString(seekBar.getProgress()));
setText(int)
方法期望整数参数指向String资源(例如R.string.whatever
,但实际需要的是字符串表示的数字。
此外,如果您希望在用户在SeekBar上移动滑块时更改文本,则需要OnSeekBarChangedListener
答案 1 :(得分:0)
尝试:
textview.setText(String.valueOf(seekBar.getProgress()));
由于getProgess()
返回一个int值,setText()
认为其资源标识符位于R.java (R.string.x)
。