Android Phonegap 和 fragment

标签 android android-layout cordova android-fragments

我正在尝试将 Phonegap CordovaWebView 实现到我的 Fragment 中,但它不起作用。

我的布局如下所示 (cordovawebview.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>

在我的 Fragment 的 onCreateView() 中,我尝试膨胀布局:

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

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

    return v;
}

也许有人有一些提示如何修复它。我总是得到这个错误:

10-25 15:52:02.839: ERROR/AndroidRuntime(2878): FATAL EXCEPTION: main
    android.view.InflateException: Binary XML file line #21: Error inflating class org.apache.cordova.CordovaWebView
    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at com.advantageframework.tabs.fragments.SampleFragmentA.onCreateView(SampleFragmentA.java:81)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
    ... 19 more
    Caused by: java.lang.NullPointerException
    at org.apache.cordova.CordovaWebView.loadConfiguration(CordovaWebView.java:643)
    at org.apache.cordova.CordovaWebView.<init>(CordovaWebView.java:131)
    ... 22 more

最佳答案

我找到了答案。如果您查看您的 logcat,您会看到它试图将 Context 转换为 CordovaInterface。上下文是 Activity 。那里失败了。您可能将 Fragment 设为 CordovaInterface。那就是我所做的。您必须将 Activity 设为 CordovaInterface 并将事件(onMessage,...)从 Activity 转发到 Fragment。

这是我的 Activity (删除了一些内容以使其更简单)。我有一些非常接近这个的东西,效果很好。之后您仍然会遇到一些小问题,但这些问题很容易解决。

public class MyCordovaActivity extends SherlockFragmentActivity implements
        CordovaInterface {
    private final ExecutorService mThreadPool = Executors.newCachedThreadPool();
    private CordovaPlugin mActivityResultCallback;

    private CordovaFragment mFragment;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nbu_web_activity);
        mFragment = new CordovaFragment();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment, mFragment)
                .commit();

        // TODO this value you could pass to the activity with a intent extra
        // or allow to do this through a seperate function, ...
        String url = "http://....";
        mFragment.loadUrl(url);
    }

    @Override
    protected void onNewIntent(final Intent intent) {
        super.onNewIntent(intent);
        NBUGapFragment fragment = getCordovaFragment();
        if (fragment != null && fragment.appView != null) {
            fragment.appView.onNewIntent(intent);
        }
    }

    @Override
    public void onBackPressed() {
        NBUGapFragment fragment = getCordovaFragment();
        if (fragment == null || !fragment.onBackPressed()) {
            super.onBackPressed();
        }
    }

    @Override
    public void cancelLoadUrl() {
        getCordovaFragment().cancelLoadUrl();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public ExecutorService getThreadPool() {
        return mThreadPool;
    }

    @Override
    public Object onMessage(final String id, final Object data) {
        return getCordovaFragment().onMessage(id, data);
    }

    @Override
    public void setActivityResultCallback(final CordovaPlugin plugin) {
        mActivityResultCallback = plugin;
    }

    @Override
    public void startActivityForResult(final CordovaPlugin plugin, final Intent intent,
            final int requestCode) {
        mActivityResultCallback = plugin;
        startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
        if (mActivityResultCallback != null) {
            mActivityResultCallback.onActivityResult(requestCode, resultCode, intent);
        } else {
            super.onActivityResult(requestCode, resultCode, intent);
        }
    }

    private CordovaFragment getCordovaFragment() {
        // the CordovaFragment is the one implementing CordovaInterface
        return mFragment;
    }
}

关于Android Phonegap 和 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13070205/

相关文章:

android - Retrofit2:检索 JSONObject 不起作用

android - 尽管使用了 adjustResize,但键盘仍显示在表单上

javascript - 可见性隐藏 vs -webkit-transform : translate3d() in phonegap application

iphone - 导航到下一页时显示白屏 iPhone

javascript - Cordova jsPDF - 生成 PDF,我在手机中找不到 pdf 文件

Android Listener 检测屏幕何时被按下

android - HTML 5 应用程序将文件下载到与应用程序相同的目录

android - 使用数据绑定(bind)库将基本类型导入布局

java - ListView 条目在滚动时会更改其布局

android - 不可见 View 的触摸能力