android - 来自Cordova插件的未捕获异常

时间:2015-05-21 19:54:49

标签: android cordova

我正在尝试将YoutubeAndroidPlayerAPI用于我的Cordova应用程序。我已成功构建项目,并且能够将数据从Javascript传递到Java,并且还能够显示Toast,但我无法启动Youtube的活动。

我的代码正在获取异常:

private void openVideo(final String videoid) {
    Intent youtubeIntent = YouTubeStandalonePlayer.createVideoIntent((Activity) this.cordova, "myapikey", videoid);
    this.cordova.startActivityForResult(this, youtubeIntent, 0);
}

来自logcat:

E/PluginManager(14974): Uncaught exception from plugin
E/PluginManager(14974): java.lang.ClassCastException: org.apache.cordova.CordovaActivity$1 cannot be cast to android.app.Activity
E/PluginManager(14974):         at YoutubePlugin.openVideo(YoutubePlugin.java:44)
E/PluginManager(14974):         at YoutubePlugin.execute(YoutubePlugin.java:30)
E/PluginManager(14974):         at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:95)
E/PluginManager(14974):         at org.apache.cordova.PluginManager.exec(PluginManager.java:130)
E/PluginManager(14974):         at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59)
E/PluginManager(14974):         at org.apache.cordova.CordovaBridge.promptOnJsPrompt(CordovaBridge.java:135)
E/PluginManager(14974):         at org.apache.cordova.engine.SystemWebChromeClient.onJsPrompt(SystemWebChromeClient.java:119)
E/PluginManager(14974):         at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:580)
E/PluginManager(14974):         at android.os.Handler.dispatchMessage(Handler.java:99)
E/PluginManager(14974):         at android.os.Looper.loop(Looper.java:130)
E/PluginManager(14974):         at android.app.ActivityThread.main(ActivityThread.java:3687)
E/PluginManager(14974):         at java.lang.reflect.Method.invokeNative(Native Method)
E/PluginManager(14974):         at java.lang.reflect.Method.invoke(Method.java:507)
E/PluginManager(14974):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
E/PluginManager(14974):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
E/PluginManager(14974):         at dalvik.system.NativeStart.main(Native Method)
D/SystemWebChromeClient(14974): : Line -1470026189 : org.apache.cordova.CordovaActivity$1
I/Web Console(14974): org.apache.cordova.CordovaActivity$1 at :-1470026189

1 个答案:

答案 0 :(得分:1)

您没有补充该方法的正确参数。另外我不相信cordova有startActivityForResult方法。

尝试以下方法。

private void openVideo(final String videoid) {
    Context mcontext=this.cordova.getActivity().getApplicationContext();
    Intent youtubeIntent = YouTubeStandalonePlayer.createVideoIntent(this.cordova.getActivity(), "myapikey", videoid);
    youtubeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mcontext.startActivity(youtubeIntent);
}