gpt4 book ai didi

java - onActivityResult 拍照时返回空指针异常?

转载 作者:行者123 更新时间:2023-12-01 15:02:14 40 4
gpt4 key购买 nike

我正在尝试将其与我的应用程序集成:Capture Image from Camera and Display in Activity

当我打开 Activity 时,我完全使用了作者所写的内容,但出现了空指针异常。 logcat 如下所示:

11-20 15:03:13.178: D/AndroidRuntime(15052): Shutting down VM
11-20 15:03:13.178: W/dalvikvm(15052): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
11-20 15:03:13.189: E/AndroidRuntime(15052): FATAL EXCEPTION: main
11-20 15:03:13.189: E/AndroidRuntime(15052): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dummies.android.taskreminder/com.dummies.android.taskreminder.activity.UsersThoughts}: java.lang.ClassCastException: android.widget.Button
11-20 15:03:13.189: E/AndroidRuntime(15052): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
11-20 15:03:13.189: E/AndroidRuntime(15052): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)

所以我尝试对其进行一些更改,现在每当我从拍照返回到我的 Activity 时,当我使用此配置时,我都会收到空指针异常:

//in oncreate       
this.imageView = new ImageView(this);
this.imageView = (ImageView) this.imageView.findViewById(R.id.camera_thought);


//method in class file
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}

错误如下所示:

11-20 14:53:39.118: D/AndroidRuntime(14931): Shutting down VM
11-20 14:53:39.118: W/dalvikvm(14931): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
11-20 14:53:39.128: E/AndroidRuntime(14931): FATAL EXCEPTION: main
11-20 14:53:39.128: E/AndroidRuntime(14931): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/89 (has extras) }} to activity {com.dummies.android.taskreminder/com.dummies.android.taskreminder.activity.UsersThoughts}: java.lang.NullPointerException
11-20 14:53:39.128: E/AndroidRuntime(14931): at android.app.ActivityThread.deliverResults(ActivityThread.java:3734)

最佳答案

问题是 imageView 在您的 onCreate() 方法之后将为 null

看起来您在分配它时混合了一些不同的约定。通常,在扩充布局文件时会调用 findViewById()。您可能有某种布局,某种 R.layout.my_layout 形式,因此请尝试先调用 setContentView(),然后找到 ImageView >:

setContentView(R.layout.my_layout)
this.imageView = (ImageView)findViewById(R.id.camera_thought);

关于java - onActivityResult 拍照时返回空指针异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13474936/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com