我正在尝试调用一个函数来停止来自另一个类的 Google VR 播放器。但是每次我得到这个错误:
Attempt to invoke virtual method 'void com.thevrplugin.cordovapluginvrplayer.MediaLoader.pause()' on a null object reference
我认为我以错误的方式调用它,你会怎么做?也许只是 private/public/protected 修饰符一团糟?
附言。您可以在此处找到演示项目:https://github.com/StarStep/cordova-vr-help.git
所以具有这些功能的类是这样的:
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();
}
}
然后从这里调用
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";
}
}
最佳答案
Activity
由 Android 框架实例化。为了在 Activity
中调用方法,您需要引用该 Activity ,但这可能会导致 memory leak .
请查看Start android activity from cordova plugin
您必须让您的 VrVideoActivity 处理所有 UI/UX 行为,然后返回到您的 Cordova WebApp。您需要了解 Java 和 Android 开发,尤其是 Activity Lifecycle
关于Android java 无法调用另一个类的函数。怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55152494/