javaandroidandroid-number-picker

Why doesn't numberpicker.getvalue() get the return value?


I want to getvalue from my numberpicker, but my numberpicker.getvalue() it's not working.

this is my java

public class MakananIndoVer1 extends AppCompatActivity {

    private CoordinatorLayout coordinatorLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_makanan_indo_ver1);

        coordinatorLayout = findViewById(R.id.CoordinatorLayout);
        final NumberPicker picker = (NumberPicker) findViewById(R.id.number_picker_default);
        picker.setMax(15);
        picker.setMin(0);
        picker.setUnit(1);
        picker.setValue(0);

        picker.setValueChangedListener(new ValueChangedListener() {
            @Override
            public void valueChanged(int value, ActionEnum action) {

                Toast.makeText(MakananIndoVer1.this, picker.getValue(), Toast.LENGTH_SHORT).show();

            }
        });
    }
}

and this is my logcat

2019-01-03 17:16:09.006 1563-3245/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 1336405 , only wrote 1336320
2019-01-03 17:16:09.009 4907-4907/com.example.denny.prodia D/AndroidRuntime: Shutting down VM


    --------- beginning of crash
2019-01-03 17:16:09.079 4907-4907/com.example.denny.prodia E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.denny.prodia, PID: 4907
    java.lang.ClassCastException: android.content.res.Resources cannot be cast to java.lang.CharSequence
        at com.example.denny.prodia.MakananIndoVer1$1.valueChanged(MakananIndoVer1.java:34)
        at com.travijuu.numberpicker.library.NumberPicker.changeValueBy(NumberPicker.java:225)
        at com.travijuu.numberpicker.library.NumberPicker.increment(NumberPicker.java:204)
        at com.travijuu.numberpicker.library.Listener.ActionListener.onClick(ActionListener.java:40)
        at android.view.View.performClick(View.java:6597)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25885)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)


    --------- beginning of system
2019-01-03 17:16:09.196 1819-4593/? W/ActivityManager:   Force finishing activity com.example.denny.prodia/.MakananIndoVer1
2019-01-03 17:16:09.222 1819-1834/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.DROPBOX_ENTRY_ADDED flg=0x10 (has extras) } to com.google.android.gms/.stats.service.DropBoxEntryAddedReceiver
2019-01-03 17:16:09.223 1819-1834/? W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.DROPBOX_ENTRY_ADDED flg=0x10 (has extras) } to com.google.android.gms/.chimera.GmsIntentOperationService$PersistentTrustedReceiver
2019-01-03 17:16:09.227 4907-4907/com.example.denny.prodia I/Process: Sending signal. PID: 4907 SIG: 9
2019-01-03 17:16:09.268 2599-2612/? W/gle.android.gm: Couldn't lock the profile file /data/misc/profiles/cur/0/com.google.android.gms/primary.prof: Failed to lock file '/data/misc/profiles/cur/0/com.google.android.gms/primary.prof': Try again
2019-01-03 17:16:09.268 2599-2612/? W/gle.android.gm: Could not forcefully load profile /data/misc/profiles/cur/0/com.google.android.gms/primary.prof
2019-01-03 17:16:09.291 1819-1895/? W/InputDispatcher: channel '62146ad com.example.denny.prodia/com.example.denny.prodia.MakananIndoVer1 (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
2019-01-03 17:16:09.291 1819-1895/? E/InputDispatcher: channel '62146ad com.example.denny.prodia/com.example.denny.prodia.MakananIndoVer1 (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-01-03 17:16:09.291 1819-1895/? W/InputDispatcher: channel 'e7decf7 com.example.denny.prodia/com.example.denny.prodia.MenuMakananUtama (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
2019-01-03 17:16:09.291 1819-1895/? E/InputDispatcher: channel 'e7decf7 com.example.denny.prodia/com.example.denny.prodia.MenuMakananUtama (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-01-03 17:16:09.291 1819-1895/? W/InputDispatcher: channel '12dd43d com.example.denny.prodia/com.example.denny.prodia.MakananKhasIndo (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
2019-01-03 17:16:09.291 1819-1895/? E/InputDispatcher: channel '12dd43d com.example.denny.prodia/com.example.denny.prodia.MakananKhasIndo (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-01-03 17:16:09.291 1819-1895/? W/InputDispatcher: channel 'eff06b com.example.denny.prodia/com.example.denny.prodia.MenuUtama (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
2019-01-03 17:16:09.291 1819-1895/? E/InputDispatcher: channel 'eff06b com.example.denny.prodia/com.example.denny.prodia.MenuUtama (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-01-03 17:16:09.293 1668-1668/? I/Zygote: Process 4907 exited due to signal (9)

in here i want to view the value from my numberpicker. but, i just know my picker.getvalue() is error and break my emulator. i just want to know where i can fix the error.


Solution

  • The method makeText() has several overloads.
    You wanted to use this:

    public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
        return makeText(context, null, text, duration);
    }
    

    but because you passed picker.getValue() which is an integer value,
    the compiler used this:

    public static Toast makeText(Context context, @StringRes int resId, @Duration int duration)
                                throws Resources.NotFoundException {
        return makeText(context, context.getResources().getText(resId), duration);
    }
    

    which as you can see takes as 2nd parameter an integer, but this integer is interpreted as a resource id.
    So the app crashes because there is no resource id with the value picker.getValue().
    You only have to make that 2nd parameter you pass to be a String:

    Toast.makeText(MakananIndoVer1.this, String.ValueOf(picker.getValue()), Toast.LENGTH_SHORT).show();
    

    or

    Toast.makeText(MakananIndoVer1.this, "" + picker.getValue(), Toast.LENGTH_SHORT).show();