I am getting error. (' android.app.ActivityThread.getApplicationThread()' on a null object reference )

426 Views Asked by At

I am trying to access a method of activity which calls the startActivityForResult method and it shows the error

I have initialized this in the onCreate method of class

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

here I am trying to access the method of mainActivity which is 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;
        }

}

It is the method of 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);
}
0

There are 0 best solutions below