Reputation: 1459
in my application one photo button is there. when I click on that button application open in camera mode and after capturing photo it will return on the layout and show that capture photo on this layout.But when the application is in camera mode and that time when I press return button then application stop working and it show :
The application "AppName" (XXX) has stopped unexpectedly [Force Close]
on the LogCat it show following error :
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=0, data=null} to activity {com.XXX.android.XXX/com.XXX.android.XXX.FirstActivity}: java.lang.NullPointerException
But here I want my application return to the layout. In my application I write following code : In main.xml I create one ImageView, one EditView, one photo button and one save button :
<ImageView
android:id="@+id/test_image"
android:src="@drawable/aas"
android:layout_width="180dp"
android:layout_height="150dp"
android:layout_below="@id/edit2"
android:layout_toRightOf="@id/main_view_id_text7"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="2dp" />
<EditText
android:id="@+id/edit2"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_below="@id/edit1"
android:layout_centerHorizontal="true"
android:layout_marginTop="9dp"
android:layout_marginRight="4dp"
android:layout_marginLeft="2dp"
android:layout_toRightOf="@id/button1"
android:layout_alignParentRight="false"
android:background="@color/light_yellow"
android:textColor="@color/darkblue"
android:inputType="text"
android:hint="Photo Name" />
<Button
android:id="@+id/button1"
android:layout_width="70dp"
android:layout_height="36dp"
android:layout_below="@id/main_view_id_text4"
android:layout_marginTop="11dp"
android:onClick="addPhoto"
android:text="@string/photo"
/>
<Button
android:id="@+id/button2"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_below="@id/test_image"
android:layout_alignParentLeft="true"
android:layout_marginTop="2dp"
android:onClick="savePhoto"
android:text="@string/save" />
Now in Activity Class code camera application :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editTextPhoto = (EditText) findViewById(R.id.edit2);
buttonPhoto = (Button) findViewById(R.id.button1);
imageView = (ImageView) findViewById(R.id.test_image);
buttonPhoto.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addPhoto();
}
});
protected void addPhoto() {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
editTextPhoto.setText(new Date().toString() + ".jpg");
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
protected void savePhoto() {
// code to save captured photo on provided location
}
I also want to show Image name on EditText (or in TextView), the image name which is captured by the Camera. By this code captured photo is stored in Gallery/Photo folder of mobile phone. One more thing I want which is when again I click on photo button the previous captured photo will delete from that folder and the captured photo will save only when I click on Save button.
One more thing please also mention how return button will handle so I use this in other part of application. In my application I collect some data. Suppose client enter some data and in the middle he/she press return button then one Alert box will come "your current data will loss what do you want [Yes] [No]" after pressing on "No" the application will stop.
List of Problem which I am facing :
Similar Question which asked earlier in this website, what they do: They Capture the photo by this code, save photo in gallery and display on the layout by ImageView but they not display the name of photo on the layout.
This message appear on the LogCat when I press return or back button of the mobile phone while application is in Camera mode:
01-23 10:47:25.839: E/AndroidRuntime(11266): FATAL EXCEPTION: main
01-23 10:47:25.839: E/AndroidRuntime(11266): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=0, data=null} to activity {com.pixel.android.locationentry/com.pixel.android.locationentry.FirstActivity}: java.lang.NullPointerException
01-23 10:47:25.839: E/AndroidRuntime(11266): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
01-23 10:47:25.839: E/AndroidRuntime(11266): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
01-23 10:47:25.839: E/AndroidRuntime(11266): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
01-23 10:47:25.839: E/AndroidRuntime(11266): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
01-23 10:47:25.839: E/AndroidRuntime(11266): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 10:47:25.839: E/AndroidRuntime(11266): at android.os.Looper.loop(Looper.java:130)
01-23 10:47:25.839: E/AndroidRuntime(11266): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-23 10:47:25.839: E/AndroidRuntime(11266): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 10:47:25.839: E/AndroidRuntime(11266): at java.lang.reflect.Method.invoke(Method.java:507)
01-23 10:47:25.839: E/AndroidRuntime(11266): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-23 10:47:25.839: E/AndroidRuntime(11266): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
01-23 10:47:25.839: E/AndroidRuntime(11266): at dalvik.system.NativeStart.main(Native Method)
01-23 10:47:25.839: E/AndroidRuntime(11266): Caused by: java.lang.NullPointerException
01-23 10:47:25.839: E/AndroidRuntime(11266): at com.pixel.android.locationentry.FirstActivity.onActivityResult(FirstActivity.java:420)
01-23 10:47:25.839: E/AndroidRuntime(11266): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
01-23 10:47:25.839: E/AndroidRuntime(11266): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
01-23 10:47:25.839: E/AndroidRuntime(11266): ... 11 more
Upvotes: 3
Views: 1884
Reputation: 68187
change your onActivityResult implementation in such a way that it can handle null response too (i.e. if you dont take pic and simply return). for example:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
if(photo != null)
imageView.setImageBitmap(photo);
}
}
Upvotes: 1
Reputation: 13541
This is your problem:
01-23 10:47:25.839: E/AndroidRuntime(11266): at com.pixel.android.locationentry.FirstActivity.onActivityResult(FirstActivity.java:420)
Analyze why its null.
Upvotes: 0