1

I'm trying to call a function to stop the Google VR player from another class. But each time I got this error:

Attempt to invoke virtual method 'void com.thevrplugin.cordovapluginvrplayer.MediaLoader.pause()' on a null object reference

I think that I'm calling it in the wrong way, how would you do it? Maybe it's just private / public / protected modifier mess?

Ps. you can find the demo project here: https://github.com/StarStep/cordova-vr-help.git

So the class which has the functions is this:

package com.thevrplugin.cordovapluginvrplayer;

...

public class VrVideoActivity extends GvrActivity {
    
    private static final String TAG = "VrVideoActivity";
    private static final int EXIT_FROM_VR_REQUEST_CODE = 42;
    private GvrView gvrView;
    private Renderer renderer;
    private VideoUiView uiView;
    protected MediaLoader mediaLoader;
    private ControllerManager controllerManager;
    private Controller controller;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mediaLoader = new MediaLoader(this);

        gvrView = new GvrView(this);
        gvrView.setRenderTargetScale(.5f);
        renderer = new Renderer(gvrView);
        gvrView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        gvrView.setRenderer(renderer);
        setContentView(gvrView);
        
    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        recreate();
    }
    
    @Override
    protected void onPause() {
        mediaLoader.pause();
        controllerManager.stop();
        super.onPause();
    }
}

and then it's called from this

public class VrPlayer extends CordovaPlugin {
    
    private String play(Context context, JSONArray args) throws JSONException {
        Intent intent = new Intent(context, VrVideoActivity.class);
        intent.setAction(Intent.ACTION_VIEW);
        JSONObject options = args.getJSONObject(0);
        intent.putExtra("src", options.getString("src"));
        intent.putExtra("displayMode", 0);
        intent.putExtra("inputFormat", 0);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        return "Ok!";
    }

    private String pause(Context context, JSONArray args) throws JSONException {
        (new VideoActivity()).onPause();
        return "true";
    }        
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
KeySee
  • 760
  • 1
  • 12
  • 26

2 Answers2

1

Activities are instantiated by the Android framework. In order to call methods in a Activity you'll need a reference to that activity, but that will probably result in memory leak.

Please review Start android activity from cordova plugin

You'll have to have your VrVideoActivity handle all UI/UX behavior and then return back to your Cordova WebApp. You'll need to understand Java and Android development especially the Activity Lifecycle

Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
  • I already got the Activity and can close and open it. About the memory leak will check it later. For now I would like to be able to stop the video...I'm preparing a demo, will add the link later to the question if you want to give a closer look. Thanks – KeySee Mar 13 '19 at 23:36
0

Solved by making the reference variable and the function static.

protected MediaLoader mediaLoader;  ---> protected static MediaLoader mediaLoader;
private String pause(Context contex... ---> public static void myPause() 

and then i were able to call it just like this: VrVideoActivity.myPause();

KeySee
  • 760
  • 1
  • 12
  • 26