Android java 无法从另一个 class 调用函数。怎么做?
Android java cant invoke a function from another class. How to do it?
我正在尝试调用一个函数来停止来自另一个 class 的 google VR 播放器。但是每次我收到这个错误:
Attempt to invoke virtual method 'void com.thevrplugin.cordovapluginvrplayer.MediaLoader.pause()' on a null object reference
我觉得我的调用方式不对,你会怎么做?也许只是 private / public / protected 修饰符一团糟?
Ps。您可以在此处找到 演示项目 :https://github.com/StarStep/cordova-vr-help.git
所以 class 的功能是这样的:
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";
}
}
Activities
由 Android 框架实例化。为了调用 Activity
中的方法,您需要引用 activity,但这可能会导致 memory leak。
请查看
您必须让您的 VrVideoActivity 处理所有 UI/UX 行为,然后 return 返回您的 Cordova WebApp。您需要了解 Java 和 Android 开发,尤其是 Activity Lifecycle
通过使引用变量和函数静态化解决。
protected MediaLoader mediaLoader; ---> protected static MediaLoader mediaLoader;
private String pause(Context contex... ---> public static void myPause()
然后我就可以这样调用它了:VrVideoActivity.myPause();
我正在尝试调用一个函数来停止来自另一个 class 的 google VR 播放器。但是每次我收到这个错误:
Attempt to invoke virtual method 'void com.thevrplugin.cordovapluginvrplayer.MediaLoader.pause()' on a null object reference
我觉得我的调用方式不对,你会怎么做?也许只是 private / public / protected 修饰符一团糟?
Ps。您可以在此处找到 演示项目 :https://github.com/StarStep/cordova-vr-help.git
所以 class 的功能是这样的:
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";
}
}
Activities
由 Android 框架实例化。为了调用 Activity
中的方法,您需要引用 activity,但这可能会导致 memory leak。
请查看
您必须让您的 VrVideoActivity 处理所有 UI/UX 行为,然后 return 返回您的 Cordova WebApp。您需要了解 Java 和 Android 开发,尤其是 Activity Lifecycle
通过使引用变量和函数静态化解决。
protected MediaLoader mediaLoader; ---> protected static MediaLoader mediaLoader;
private String pause(Context contex... ---> public static void myPause()
然后我就可以这样调用它了:VrVideoActivity.myPause();