在上下文中获取NullPointerException

问题描述 投票:0回答:1

我想在Android Studio中运行该程序,但是我收到了这个错误。

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
    at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:152)
    at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
    at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:224)
    at android.app.AlertDialog$Builder.<init>(AlertDialog.java:454)
    at com.example.drowzy.LivePreviewActivity.drowzy_alert(LivePreviewActivity.java:219)
    at com.example.drowzy.LivePreviewActivity.eye_tracking(LivePreviewActivity.java:212)
    at com.example.drowzy.FaceDetectionProcessor.onSuccess(FaceDetectionProcessor.java:90)
    at com.example.drowzy.FaceDetectionProcessor.onSuccess(FaceDetectionProcessor.java:38)
    at com.example.drowzy.VisionProcessorBase$2.onSuccess(VisionProcessorBase.java:114)
    at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:251)
    at android.app.ActivityThread.main(ActivityThread.java:6572)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

它说我的上下文是空的。我在谷歌尝试了可能的解决方案,但它无法正常工作。我初始化上下文的方法是错误的吗?我可以在FaceDetectionProcessor中没有指定上下文的情况下在另一个类中调用drowzy_alert方法吗?

这是我的LivePreviewActivity

public void eye_tracking(@NonNull FirebaseVisionFace face){
    if (face.getRightEyeOpenProbability() < 0.1 && face.getLeftEyeOpenProbability() < 0.1) {
        drowzy_alert();
    }
}

public void drowzy_alert(){

    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Drowzy Detected")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // FIRE ZE MISSILES!
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
    // Create the AlertDialog object and return it
    builder.create();
}

这是我的FaceDetectionProcessor

protected void onSuccess(
        @Nullable Bitmap originalCameraImage,
        @NonNull List<FirebaseVisionFace> faces,
        @NonNull FrameMetadata frameMetadata,
        @NonNull GraphicOverlay graphicOverlay
) {

    graphicOverlay.clear();
    if (originalCameraImage != null) {
        CameraImageGraphic imageGraphic = new CameraImageGraphic(graphicOverlay, originalCameraImage);
        graphicOverlay.add(imageGraphic);
    }
    for (int i = 0; i < faces.size(); ++i) {
        FirebaseVisionFace face = faces.get(i);

        int cameraFacing =
                frameMetadata != null ? frameMetadata.getCameraFacing() :
                        Camera.CameraInfo.CAMERA_FACING_BACK;
        FaceGraphic faceGraphic = new FaceGraphic(graphicOverlay, face, cameraFacing);
        LivePreviewActivity livePreviewActivity = new LivePreviewActivity();
        graphicOverlay.add(faceGraphic);
        livePreviewActivity.eye_tracking(face);
    }
    graphicOverlay.postInvalidate();
}
java android android-context firebase-mlkit
1个回答
1
投票
LivePreviewActivity livePreviewActivity = new LivePreviewActivity();

您无法以这种方式实例化LivePreviewActivity。如果您有活动,则只能使用startActivity或其他由android管理的上下文方法。

整个想法是,无论何时使用活动,活动应位于活动堆栈之上(即对用户可见),startActivity会为您执行此操作。它还会在您的活动中传递应用程序和活动上下文,应用程序需要知道应用程序的状态以及设备。没有此上下文,应用程序无法工作。

您的活动需要位于堆栈顶部的原因是,一旦活动不再位于堆栈的顶部,它可以在任何时候被android杀死以节省内存,并且您无法阻止该行为。您可以对被杀死的活动作出反应,但不能超过该活动。

根据我收集的内容,您必须将所有这些逻辑移动到单个活动中,而不是将其分成两个。如果您确实想要同时加载多个活动,请考虑使用片段。

© www.soinside.com 2019 - 2024. All rights reserved.