我正試圖將其與我的應用程序集成:Capture Image from Camera and Display in ActivityonActivityResult在拍攝照片時返回空指針異常?
當我打開活動時,我正是使用作者寫的東西,我得到一個空指針異常。這裏是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)
嗯,我在嘗試定義imageview之前調用setContentView。它仍然給我一個空指針異常。 : -/ – Stagleton
確保您的佈局具有帶有屬性「android:id =」@ + id/camera_thought「'的ImageView。 – wsanville
是的,我有這個在我的佈局: –
Stagleton