gpt4 book ai didi

java - 为什么 numberpicker.getvalue() 没有得到返回值?

转载 作者:行者123 更新时间:2023-12-02 01:43:47 30 4
gpt4 key购买 nike

我想从我的 numberpicker 中获取值,但我的 numberpicker.getvalue() 不起作用。

这是我的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();

}
});
}
}

这是我的 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)

在这里我想查看我的数字选择器中的值。但是,我只知道我的 picker.getvalue() 是错误的并破坏了我的模拟器。我只是想知道在哪里可以修复错误。

最佳答案

方法 makeText() 有多个重载。
您想使用这个:

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

但是因为您传递了 picker.getValue() 这是一个整数值,
编译器使用了这个:

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

如您所见,它采用一个整数作为第二个参数,但该整数被解释为资源 ID。
因此应用程序崩溃,因为没有值为 picker.getValue() 的资源 ID。
您只需将传递的第二个参数设为String:

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

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

关于java - 为什么 numberpicker.getvalue() 没有得到返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54020449/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com