尝试通过Java类访问活动中的方法时,在空对象引用上出现android.content.Context.getPackageName()'

时间:2019-04-15 22:22:09

标签: java firebase android-intent firebase-mlkit

我正在实时运行一个基本的人脸检测应用程序,并在FaceDetectionProcessor.java类(使用它作为参考-> https://github.com/firebase/quickstart-android/tree/master/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/java)中并在特定的时间计数它检测到人脸的次数。计数值,它使用FaceDetectionProcessor.java对象使Activity成为对象,从而调用方法来启动新的活动,然后崩溃。

尝试了FaceDetectActivity.this,简单来说就是getApplicationContext(),甚至是一个全局Context对象,它在FaceDetectActivity的onCreate()中将其值设置为“ this”

public class FaceDetectActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback,CompoundButton.OnCheckedChangeListener{
    private static final String FACE_DETECTION = "Face Detection";
    private static final String TAG = "LivePreviewActivity";
    private static final int PERMISSION_REQUESTS = 1;
    private CameraSource cameraSource = null;
    private CameraSourcePreview preview;
    private GraphicOverlay graphicOverlay;
    private String selectedModel = FACE_DETECTION;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
.
.
.
.
}


private void createCameraSource(String model) {
        // If there's no existing cameraSource, create one.
        if (cameraSource == null) {
            cameraSource = new CameraSource(this, graphicOverlay);
        }
        try {
            switch (model) {
                case FACE_DETECTION:
                    Log.i(TAG, "Using Face Detector Processor");
                    cameraSource.setMachineLearningFrameProcessor(new FaceDetectionProcessor());
                    break;
                default:
                    Log.e(TAG, "Unknown model: " + model);
            }
        } catch (Exception e) {
            Log.e(TAG, "can not create camera source: " + model);
        }
    }

public void alertAndMove(){
        Intent i=new Intent(FaceDetectActivity.this,SelectingParty.class);
        startActivity(i);
    }
public class FaceDetectionProcessor extends VisionProcessorBase<List<FirebaseVisionFace>> {
    int count=0;

    private static final String TAG = "FaceDetectionProcessor";

    private final FirebaseVisionFaceDetector detector;

    public FaceDetectionProcessor() {
        FirebaseVisionFaceDetectorOptions options =
                new FirebaseVisionFaceDetectorOptions.Builder()
                        .setClassificationMode(FirebaseVisionFaceDetectorOptions.ALL_CLASSIFICATIONS)
                        .enableTracking()
                        .build();

        detector = FirebaseVision.getInstance().getVisionFaceDetector(options);
    }

    .
    .
    .
    . 
    . 
    .
    .

    @Override
    protected void onSuccess(
            @NonNull List<FirebaseVisionFace> faces,
            @NonNull FrameMetadata frameMetadata,
            @NonNull GraphicOverlay graphicOverlay) {
        graphicOverlay.clear();
        for (int i = 0; i < faces.size(); ++i) {
            FirebaseVisionFace face = faces.get(i);
            FaceGraphic faceGraphic = new FaceGraphic(graphicOverlay);
            graphicOverlay.add(faceGraphic);
            faceGraphic.updateFace(face, frameMetadata.getCameraFacing());
            Log.d(TAG, "onSuccess: Face detected and count= "+count);
            count++;
            if(count>=15)
                new FaceDetectActivity().alertAndMove();
        }
    }

    @Override
    protected void onFailure(@NonNull Exception e) {
        Log.e(TAG, "Face detection failed " + e);
    }
}

当调用new FaceDetectActivity()。alertAndMove()时,该应用程序在Intent i = new Intent(FaceDetectActivity.this,......)行的alertAndMove()中崩溃

使这种情况非常令人沮丧的是,在我最初测试该逻辑的朋友笔记本电脑上,相同的逻辑可以在相同的代码上工作。

1 个答案:

答案 0 :(得分:0)

在FaceDetectActivity中,我将Intent和上下文发送到FaceDetectionProcessor.java:

case FACE_DETECTION:
                    Log.i(TAG, "Using Face Detector Processor");
                    Intent i=new Intent( this,SelectingParty.class);
                    cameraSource.setMachineLearningFrameProcessor(new FaceDetectionProcessor(this,i));
                    break;

然后在FaceDetectionProcessor.java的构造函数中,按如下所示初始化实例Context obj和Intent intent:

public FaceDetectionProcessor(Context obj, Intent intent) {
        this.obj=obj;
        this.intent=intent;
.
.
.

最后在FaceDetectionProcessor.java中的onSuccess方法中,我做了:

if(count>=15){
                obj.startActivity(intent);
            }

就是这样!我但是仍然无法结束以前的活动,任何暗示我将如何实现的评论都将大有帮助! 附言我尝试了CLEAR_TASK,CLEAR_TOP和NEW_TASK,但没有用,但是我可能会丢失一些东西。