调用 getData 时使用相机意图出现空指针异常

发布于 2024-10-13 05:16:31 字数 1600 浏览 7 评论 0原文

我正在使用本教程/代码来学习相机功能: 摄像头教程。

摄像头 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最冷一天 2024-10-20 05:16:31

您的代码抛出 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 call getData().getLastPathSegment(), the method getLastPathSegment() 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 if getData() is null as well.

追我者格杀勿论 2024-10-20 05:16:31

好吧,如果 data 不为 null,data.getData() 仍然可以为 null,并且您无法对 null 调用 getLastPathSegment() 。也许首先检查一下,如果是这种情况,也许正如@willytate评论的那样,返回的数据有问题吗?

Well, if data is not null, data.getData() can still be null, and you can't call getLastPathSegment() on null. Maybe check that first, and if that's the case, maybe as @willytate commented, there is something wrong with the returned data?

双手揣兜 2024-10-20 05:16:31

检查 resultCode 和 requestCode,如果为 null,则避免使用它们

if (resultCode == RESULT_OK && 数据!= null)

Check for the resultCode and requestCode avoiding them if null

if (resultCode == RESULT_OK && data != null)

强辩 2024-10-20 05:16:31

使用相机意图捕获图像后返回时,有时它不会返回数据,因此您必须使用

        Bundle extras = data.getExtras();

            Bitmap imageBitmap = (Bitmap) extras.get(TAG_data);

On returning after capturing image using camera intent sometimes it does not returns us data so that you have to use

        Bundle extras = data.getExtras();

            Bitmap imageBitmap = (Bitmap) extras.get(TAG_data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文