0

我正在尝试访问调用 startActivityForResult 方法的活动方法并显示错误

我已经在类的 onCreate 方法中初始化了它

enter code here
mediaProjectionManager = (MediaProjectionManager) this.getSystemService(Context.MEDIA_PROJECTION_SERVICE);

在这里,我试图访问 mainActivity 的方法,即 startRecording。

public void onClick(View v) {
    switch (v.getId()) {

        case R.id.record_btn: {
            mainActivity = new MainActivity();

            if (!recording) {
                expandedView.setVisibility(View.INVISIBLE);
                collapsedView.setVisibility(View.VISIBLE);
                mainActivity.startRecording(mediaProjectionManager);
                Toast.makeText(getApplicationContext(), "start recording", Toast.LENGTH_SHORT).show();
                recording = true;
            }
            else {
                notificationManager.cancel(1);
                stopRecording();

                Toast.makeText(getApplicationContext(), "stop recording", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(FloatingWidgetService.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                stopSelf();
                recording = false;
                startActivity(intent);
            }
            break;
        }

}

这是mainActivity的方法

public void startRecording(MediaProjectionManager mediaProjectionManager) {
     this.mediaProjectionManager = mediaProjectionManager;
    if (mediaProjection == null) {
        startActivityForResult(this.mediaProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
        return;
    }
    virtualDisplay = createVirtualDisplay();
    mediaRecorder.start();
}

private VirtualDisplay createVirtualDisplay() {
    return mediaProjection.createVirtualDisplay("C",
            cw, ch, metricsDensity,
            DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
            mediaRecorder.getSurface(), null , null);
}
4

0 回答 0