简体   繁体   中英

onActivityResult returning a nullpointer exception when taking a photo?

I am trying to integrate this with my application: Capture Image from Camera and Display in Activity

When I open the activity I using exactly what the author wrote I get a null pointer exception. Here is what the logcat looks like:

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)

So I tried to change it a little and now whenever I return to my activity from taking a photo I get a null pointer exception when I use this configuration:

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

Here is what the error looks like:

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)

The problem is that imageView will be null after your onCreate() method.

It looks like you're mixing a few different conventions when you assign it. Typically, calling findViewById() is used when you're inflating a layout file. You probably have some sort of layout, something of the form R.layout.my_layout , so try calling setContentView() first, then find the ImageView :

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

I can't understand this:

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

First line creates a brand new ImageView which contains nothing. Second line tries to get an ImageView which is inside the first ImageView (?).

If you want to show a xml-declared imageView, the procedure is this:

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

If you want to create it programatically:

this.imageView = new ImageView(this);

However, to use that ImageView, you'll need to set it as the content view of the Activity, or to add it to a ViewGroup which is the content View of the Activity.

setContentVIew(imageView);

or

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

I am providing you complete class, do this way:

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);
}
}

where "main" is the xml file that should contain Button "b" and ImageView "image".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM