Reputation: 21
how do I resolve the error above.Am creating a simple Android App to that grants me access to my phone's camera.Everything works fine,however,when I go back to the main activity(in the App) the App crashes.The Logcat returns this error:Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
and honestly I have no idea what it means.I have tried researching the error but every solution I find is somewhat unrelated to what am doing,though the error is still the same.The problem is said to be on this line Bitmap bitmap = (Bitmap) data.getExtras().get("");
.Below is the code that am working with:
Manifest File:AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cameraapp1">
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".TakePicture">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
XML : activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp"
tools:context=".TakePicture">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/image_view"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt_open"
android:text="Open Camera"
android:layout_marginTop="10dp"
/>
</LinearLayout>
Java File :TakePicture.java
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class TakePicture extends AppCompatActivity {
private ImageView imageView;
private Button btOpen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view);
btOpen = findViewById(R.id.bt_open);
//Request Camera Permission
if (ContextCompat.checkSelfPermission(TakePicture.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(TakePicture.this,
new String[]{
Manifest.permission.CAMERA
}, 100);
}
btOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 100);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
Bitmap bitmap = (Bitmap) data.getExtras().get("");
imageView.setImageBitmap(bitmap);
}
}
}
Logcat:
03-23 18:51:46.100 32127-32203/com.example.cameraapp1 E/GED: Failed to get GED Log Buf, err(0)
03-23 18:51:46.290 32127-32127/com.example.cameraapp1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cameraapp1, PID: 32127
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=0, data=null} to activity {com.example.cameraapp1/com.example.cameraapp1.TakePicture}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3839)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3882)
at android.app.ActivityThread.access$1300(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1519)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at com.example.cameraapp1.TakePicture.onActivityResult(TakePicture.java:52)
at android.app.Activity.dispatchActivityResult(Activity.java:6294)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3835)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3882)
at android.app.ActivityThread.access$1300(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1519)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Upvotes: 0
Views: 971
Reputation: 21
I've added resultCode == RESULT_OK
and it worked.Alternatively resultCode != RESULT_CANCELED
also worked.
if (requestCode == 100 && resultCode == RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("");
imageView.setImageBitmap(bitmap);
}
Upvotes: 0
Reputation: 627
Try this :
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode != RESULT_CANCELED){
if (requestCode == CAMERA_REQUEST) {
Bundle bundle = data.getExtras();
final Bitmap bitmap = (Bitmap) bundle.get("data");
imageView.setImageBitmap(bitmap);
}
}}
Upvotes: 0
Reputation: 444
It's obvious error. You got null (no data) here : data.getExtras().get("") Add this
if(data.getExtras() != null) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
Upvotes: 0
Reputation: 627
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode != RESULT_CANCELED){
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
Upvotes: 1