为什么 numberpicker.getvalue() 没有得到 return 值?
Why doesn't numberpicker.getvalue() get the return value?
我想从我的号码选择器中获取值,但是我的 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.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.access00(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() 出错并破坏了我的模拟器。我只想知道在哪里可以修复错误。
我自己试了一下,发现了问题。
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 .而且您可以轻松找到问题,希望对以后的问题有所帮助,祝您好运并继续编码:)
方法 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();
我想从我的号码选择器中获取值,但是我的 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.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.access00(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() 出错并破坏了我的模拟器。我只想知道在哪里可以修复错误。
我自己试了一下,发现了问题。 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 .而且您可以轻松找到问题,希望对以后的问题有所帮助,祝您好运并继续编码:)
方法 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();