user3741540
user3741540

Reputation: 93

Find progress of seekbar

My goal is to find the amount of a SeekBar that the user has selected (out of 100), and display this amount in a TextView. This seemed straight forward, but for some reason does not work with my method (shown below)

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());
    }

}

However, the app crashes (the LogCat is given below). I have identified that the error comes from this line of code

        textview.setText(seekBar.getProgress());

Why does this line of code cause the error? Is there anything I can do to make this work?

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

Upvotes: 0

Views: 136

Answers (2)

Ryhan
Ryhan

Reputation: 1885

Try:

textview.setText(String.valueOf(seekBar.getProgress()));

Since getProgess() returns an int value, setText() thinks its a resource identifier living in R.java (R.string.x).

Upvotes: 0

Karakuri
Karakuri

Reputation: 38595

You need

textView.setText(Integer.toString(seekBar.getProgress()));

The setText(int) method expects the integer argument to point to a String resource (e.g. R.string.whatever, but what you actually want is a String representation of a number.

Also, if you want the text to change when the user moves the scrubber on the SeekBar, you will need an OnSeekBarChangedListener

Upvotes: 1

Related Questions