androidgoogle-vrgoogle-vr-sdk

Android java can't invoke a function from another class. How to do it?


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";
    }        
}

Solution

  • 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();