我想从我的数字选择器中获取值,但是我的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()错误并且破坏了我的模拟器。我只想知道在哪里可以纠正错误。
答案 0 :(得分:1)
我独自尝试,发现了问题。 Toast期望获得另一种价值,您可以使用以下方法加以解决:
Toast.makeText(MakananIndoVer1.this, String.ValueOf(picker.getValue()), Toast.LENGTH_SHORT).show();
这是我的代码,带有一个有效的数字选择器:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final NumberPicker picker = (NumberPicker) findViewById(R.id.number_picker_default);
picker.setMaxValue(15);
picker.setMinValue(0);
picker.setValue(0);
picker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
Toast.makeText(MainActivity.this, String.valueOf(picker.getValue()), Toast.LENGTH_SHORT).show();
}
});
}
但是,如果您检查logcat,则会写到: java.lang.ClassCastException:android.content.res.Resources无法转换为java.lang.CharSequence 。您可以轻松找到问题,希望对以后的问题有所帮助,祝您好运并继续编码:)
答案 1 :(得分:1)
方法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();