调用 getData 时使用相机意图出现空指针异常
我正在使用本教程/代码来学习相机功能: 摄像头教程。
摄像头 Intent 拍摄照片并返回到 onActivityResult
后,应用程序崩溃。但我正在检查以确保数据不为空:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("***** inside onActivityResult");
if (requestCode == TAKE_PICTURE) {
if (data != null) {
System.out.println("***** inside data !=null if");
imageid = data.getData().getLastPathSegment(); //returns full pic id
System.out.println("***** imageid:" + imageid);
[...]
我进入了 data != null
if 语句,当我 imageid = data.getData().getLastPathSegment();< /代码>。
日志猫:
01-20 12:45:02.678: 错误/AndroidRuntime(1626): 致命异常: main 01-20 12:45:02.678:错误/AndroidRuntime(1626):java.lang.RuntimeException:传递结果失败 ResultInfo{who=null,request=1,result=-1,data=Intent { act=inline-data (有额外的)}}活动{org.kimile/org.kimile.Camera}:java.lang.NullPointerException 01-20 12:45:02.678:错误/AndroidRuntime(1626):在android.app.ActivityThread.deliverResults(ActivityThread.java:3515) 01-20 12:45:02.678:错误/AndroidRuntime(1626):在android.app.ActivityThread.handleSendResult(ActivityThread.java:3557) 01-20 12:45:02.678: 错误/AndroidRuntime(1626): 在 android.app.ActivityThread.access$2800(ActivityThread.java:125) 01-20 12:45:02.678: 错误/AndroidRuntime(1626): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
即使知道我正在检查,我也无法弄清楚为什么它会抛出 null 异常确保它不为空。
I am using this tutorial / code to learn the camera functions: cam tutorial.
The app is crashing after the camera Intent takes the picture and returns to onActivityResult
. But I am checking the make sure data is not null:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("***** inside onActivityResult");
if (requestCode == TAKE_PICTURE) {
if (data != null) {
System.out.println("***** inside data !=null if");
imageid = data.getData().getLastPathSegment(); //returns full pic id
System.out.println("***** imageid:" + imageid);
[...]
I get inside the data != null
if statement and it crashes when I imageid = data.getData().getLastPathSegment();
.
Logcat:
01-20 12:45:02.678: ERROR/AndroidRuntime(1626): FATAL EXCEPTION: main
01-20 12:45:02.678: ERROR/AndroidRuntime(1626): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {org.kimile/org.kimile.Camera}: java.lang.NullPointerException
01-20 12:45:02.678: ERROR/AndroidRuntime(1626): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
01-20 12:45:02.678: ERROR/AndroidRuntime(1626): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
01-20 12:45:02.678: ERROR/AndroidRuntime(1626): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
01-20 12:45:02.678: ERROR/AndroidRuntime(1626): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
I can't figure out why its throwing the null exception even know I am checking to make sure its not null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码抛出 NullPointerException,因为从
data.getData()
返回的对象为 null。因此,当您调用getData().getLastPathSegment()
时,将在 null 对象上调用getLastPathSegment()
方法 - 为您提供 NPE。就像 willytate 所说,如果您想完全避免该问题,请确保检查
resultCode
的值。否则,请确保检查getData()
是否为 null。Your code is throwing a NullPointerException because the object returned from
data.getData()
is null. Thus when you callgetData().getLastPathSegment()
, the methodgetLastPathSegment()
is being called on a null object - giving you your NPE.Like willytate said, if you want to avoid the problem all together make sure you check the value of
resultCode
. Otherwise, make sure you check ifgetData()
is null as well.好吧,如果
data
不为 null,data.getData()
仍然可以为 null,并且您无法对 null 调用getLastPathSegment()
。也许首先检查一下,如果是这种情况,也许正如@willytate评论的那样,返回的数据有问题吗?Well, if
data
is not null,data.getData()
can still be null, and you can't callgetLastPathSegment()
on null. Maybe check that first, and if that's the case, maybe as @willytate commented, there is something wrong with the returned data?检查 resultCode 和 requestCode,如果为 null,则避免使用它们
if (resultCode == RESULT_OK && 数据!= null)
Check for the resultCode and requestCode avoiding them if null
if (resultCode == RESULT_OK && data != null)
使用相机意图捕获图像后返回时,有时它不会返回数据,因此您必须使用
On returning after capturing image using camera intent sometimes it does not returns us data so that you have to use