onActivityResult在拍照时返回nullpointer异常?

时间:2012-11-20 14:06:16

标签: java android

我正在尝试将其与我的应用程序集成:Capture Image from Camera and Display in 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)

所以我尝试稍微改变它,现在每当我回到我的活动拍摄照片时,我使用此配置时会得到一个空指针异常:

//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)

3 个答案:

答案 0 :(得分:1)

问题是,imageView方法后null将为onCreate()

在分配时,您似乎混合了一些不同的约定。通常,在您对布局文件进行充气时会使用调用findViewById()。您可能有某种布局,格式为R.layout.my_layout,因此请先尝试调用setContentView(),然后找到ImageView

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

答案 1 :(得分:0)

我无法理解:

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

第一行创建一个全新的ImageView,其中不包含任何内容。第二行尝试获取第一个ImageView(?)内的ImageView。

如果要显示xml声明的imageView,过程如下:

//this actually refers to the activity
this.imageView = (ImageView) this.findViewById(R.id.camera_thought);

如果您想以编程方式创建它:

this.imageView = new ImageView(this);

但是,要使用该ImageView,您需要将其设置为活动的内容视图,或将其添加到ViewGroup,该视图是活动的内容视图。

setContentVIew(imageView);

viewGroup.addView(imageView);
setContentView(viewGroup);

答案 2 :(得分:0)

我为你提供完整的课程,这样做:

public class CameraActivity extends Activity {

private static final int CAMERA_PIC_REQUEST = 2500;

    @Override
public void onCreate(Bundle savedInstanceState) {          
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    Button b = (Button)findViewById(R.id.CamOpen);
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                 startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
             Bitmap image = null;
            try{
            image = (Bitmap) data.getExtras().get("data");
          }

          catch(Exception e){}
         ImageView imageview = (ImageView) findViewById(R.id.ImageView);
          imageview.setImageBitmap(image);
    }

    super.onActivityResult(requestCode, resultCode, data);
}
}

其中“main”是应包含Button“b”和ImageView“image”的xml文件。