Android 使用JavascriptInterface的Webview在调用Intent时崩溃
我有一个带有webview的应用程序,它有一个自定义JavascriptInterface。具体内容如下:Android 使用JavascriptInterface的Webview在调用Intent时崩溃,android,android-fragments,webview,youtube-api,Android,Android Fragments,Webview,Youtube Api,我有一个带有webview的应用程序,它有一个自定义JavascriptInterface。具体内容如下: class CustomWebView extends WebView{ public void prepare(){ this.addJavascriptInterface(new Interface(this.inner_c), "ifc"); } } 在javascript上,从该接口调用一个方法,该接口执行以下操作: Intent intent = Yo
class CustomWebView extends WebView{
public void prepare(){
this.addJavascriptInterface(new Interface(this.inner_c), "ifc");
}
}
在javascript上,从该接口调用一个方法,该接口执行以下操作:
Intent intent = YouTubeStandalonePlayer.createPlaylistIntent((Activity) mContext, "key", "playlist");
mContext.startActivity(intent);
一旦调用了intent,现在位于后台的应用程序就会崩溃,并引用下一行来自另一个文件的NullPointerException:
public class MiscellaneousFragment extends Fragment implements PageActionListener
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
mContext = container.getContext();
...
}
}
我怎样才能避免撞车
谢谢 您可以使用它添加到Interface.java中
new Handler.post(new Runnable() {
public void run() {
Intent intent = YouTubeStandalonePlayer.createPlaylistIntent((Activity) mContext, "key", "playlist");
mContext.startActivity(intent);
}
});
请注意,
JavascriptInterface
不会在主UI线程中执行,而在Android中,某些操作(如更新视图)必须在主线程中调用。因此,您可以在JavascriptInterface中进行检查,查看是否有任何代码违反了规则