java - Android 从枚举中获取结果
问题描述
我正在尝试获取我在此处传递的枚举的值:
Intent intent = new Intent(MainActivity.this, SelectBrush.class);
Bundle bundle = new Bundle();
bundle.putString("Current brush", myFingerPainter.getBrush().name());
intent.putExtras(bundle);
startActivityForResult(intent, BRUSH_REQUEST_CODE );
我试着在第二个活动中把它弄出来:
Paint.Cap brushStyle = Paint.Cap.valueOf(getIntent().getStringExtra("Current brush"));
这是我的日志:
Process: com.example.myapplication, PID: 26592
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myapplication/com.example.myapplication.SelectBrush}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at com.example.myapplication.SelectBrush.<init>(SelectBrush.java:17)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
我也试过
Bundle bundle = getIntent().getExtras();
String brushString = bundle.getString("Current brush");
Paint.Cap brushStyle = Paint.Cap.valueOf(brushString);
但我有一个类似的例外:
2020-10-31 11:55:38.921 28148-28148/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 28148
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myapplication/com.example.myapplication.SelectBrush}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at com.example.myapplication.SelectBrush.<init>(SelectBrush.java:16)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
我已经阅读过,我认为这是因为第二个活动认为 intent == null 但我不确定为什么,因为我显然是在第一堂课中创建它。
解决方案
只需将这些行放在 onCreate 方法中:
String brushString = bundle.getString("Current brush");
brushStyle = Paint.Cap.valueOf(brushString);
这在课堂上:
Paint.Cap brushStyle;
推荐阅读
- api - 在 Golang 中使用任何类型的加密(AES 等)加密和解密 JWT
- python - 如何将 DRF 序列化器与 Graphene 一起使用
- python - 解析此 xml 文件时产生此 KeyError 的原因是什么?
- android - 在移动到下一个活动之前完整地运行一个函数
- python - 无法使用 python 在 RaspberryPi 中的 MySQL 表中保存数据
- python - 是否可以在熊猫中使用自定义过滤器功能?
- django - Django2 ListView 使用两个模型
- sql - FOR JSON PATH 返回嵌套对象的转义字符串
- google-bigquery - 大型 bigquery 加载作业的最可靠格式
- powershell - 通过一系列 Web 服务器远程部署代码