【问题标题】:CordovaWebView inside a Fragment returns NullPointerException片段内的 CordovaWebView 返回 NullPointerException
【发布时间】:2012-12-30 05:32:28
【问题描述】:

我正在尝试在片段中实现 CordovaWebView (2.2.0),但在尝试扩展布局时返回错误。

我的 xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">

<org.apache.cordova.CordovaWebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id = "@+id/mainView"/>

</FrameLayout>

我的 onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.cordovawebview, container); // <--- the error occurs here!

    return v;
}

错误:

10-25 15:52:02.839: 错误/AndroidRuntime(2878): 致命 例外:主要 android.view.InflateException: Binary XML file line #21: Error inflating class org.apache.cordova.CordovaWebView NullPointerException CordovaWebView.java 第 691 行

int id = getResources().getIdentifier("config", "xml", this.cordova.getActivity().getPackageName());

这里的资源好像是空的。

我已经检查过了,config.xml文件在xml文件夹中。

我可以在 FragmentActivity 中实现它,但我需要在 Fragment 中实现它。

有什么想法吗?

提前致谢。

【问题讨论】:

    标签: android android-fragments android-webview cordova-2.0.0


    【解决方案1】:
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    
        View v = inflater.inflate(R.layout.cordovawebview, container, false); // <--- add false parameter
        CordovaWebView webView = (CordovaWebView)v.findViewById(R.id.mainView);
    
        return v;
    }
    

    【讨论】:

      【解决方案2】:

      需要在片段所在的活动以及片段本身上创建 CordovaWebViews。您可以创建自己的 ContextWrapper 并将其提供给您的充气机来为您处理此操作。

      这里是如何做到这一点的分步指南:https://github.com/Adobe-Marketing-Cloud/app-sample-android-phonegap/wiki/Embed-Webview-in-Android-Fragment

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-02
        相关资源
        最近更新 更多