0

我正在尝试将此与我的应用程序集成: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)
4

3 回答 3

1

问题是这imageViewnull在你的onCreate()方法之后。

看起来你在分配它时混合了一些不同的约定。findViewById()通常,在您对布局文件进行膨胀时会使用调用。你可能有某种布局,某种形式的东西R.layout.my_layout,所以先尝试调用setContentView(),然后找到ImageView

setContentView(R.layout.my_layout)
this.imageView = (ImageView)findViewById(R.id.camera_thought);
于 2012-11-20T14:12:55.133 回答
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,您需要将其设置为 Activity 的内容视图,或者将其添加到作为 Activity 内容视图的 ViewGroup 中。

setContentVIew(imageView);

或者

viewGroup.addView(imageView);
setContentView(viewGroup);
于 2012-11-20T14:17:49.403 回答
0

我正在为您提供完整的课程,请这样做:

public class CameraActivity extends Activity {

私有静态最终 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 文件。

于 2012-11-20T14:32:42.913 回答