我如何解决上述错误。我正在创建一个简单的Android应用程序,该应用程序可以授予我使用手机摄像头的权限。但是,一切正常,但是,当我返回到该应用程序中的主要活动时,该应用程序崩溃了。Logcat返回这个错误:Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference,实际上我不知道这意味着什么。我尝试研究该错误,但是我发现的每个解决方案都与正在执行的操作无关,尽管该错误仍然相同。这行Bitmap bitmap = (Bitmap) data.getExtras().get("");。下面是正在使用的代码:

清单文件: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文件: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) 

最佳答案

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


}

10-06 01:25