0

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);
}
  • which line throws the Exception? Basically, this is a NullPointerException, so somewhere, some variable wasn't initialized – Stultuske May 22 '19 at 05:50
  • `mainActivity = new MainActivity();` is not right way to access method from other Activity. show full crash logs – ρяσѕρєя K May 22 '19 at 05:50
  • java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference at android.app.Activity.startActivityForResult(Activity.java:4473) – Muhammad Salman May 22 '19 at 07:30
  • at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767) at android.app.Activity.startActivityForResult(Activity.java:4430) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754) at com.solutionsoul.recordactions.MainActivity.startRecording(MainActivity.java:227) at com.solutionsoul.recordactions.FloatingWidgetService.onClick(FloatingWidgetService.java:384) – Muhammad Salman May 22 '19 at 07:31
  • this line throws Exception : startActivityForResult(this.mediaProjectionManager.createScreenCaptureIntent(), REQUEST_CODE); – Muhammad Salman May 22 '19 at 07:32

0 Answers0