4

Phonegap CordovaWebViewをフラグメントに実装しようとしていますが、機能しません。

私のレイアウトは次のようになります(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>

My 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
4

3 に答える 3

7

私は答えを見つけました。ログキャットを見ると、コンテキストをCordovaInterfaceにキャストしようとしていることがわかります。コンテキストはアクティビティです。そこで失敗します。おそらくフラグメントをCordovaInterfaceにしました。それが私がしたことです。アクティビティをCordovaInterfaceにして、アクティビティからイベント(onMessage、...)をフラグメントに転送する必要があります。

これが私のアクティビティです(簡単にするためにいくつかのものを削除しています)。私はこれに非常に近いものがあり、かなりうまく機能します。その後もいくつかの小さな問題が発生しますが、それらは簡単に解決できます。

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;
    }
}
于 2012-11-02T00:03:28.260 に答える
2

CordovaWebViewは、Inflaterのコンテキスト(この場合はメインアクティビティ)がCordovaInterfaceを実装していることを前提としています。インフレータを介して個別のオブジェクト(コンテキストとCordovaInterfaceオブジェクト)を渡すインターフェイスはありません。2つのオブジェクトを含むプロキシコンテキストを作成し、それぞれにメソッド呼び出しを委任する必要があります。

private class CordovaContext extends ContextWrapper implements CordovaInterface
{
    CordovaInterface ci;

    // Hold two objects. Method call to Context will be proxied by ContextWrapper, so only delegate CordovaInterface.
    // You should add/modify when method in CordovaInterface changed
    public CordovaContext(Context base, CordovaInterface ci) {
        super(base);
        this.ci = ci;
    }
    public void startActivityForResult(CordovaPlugin command,
            Intent intent, int requestCode) {
        ci.startActivityForResult(command, intent, requestCode);
    }
    public void setActivityResultCallback(CordovaPlugin plugin) {
        ci.setActivityResultCallback(plugin);
    }
    public Activity getActivity() {
        return ci.getActivity();
    }
    public Object onMessage(String id, Object data) {
        return ci.onMessage(id, data);
    }
    public ExecutorService getThreadPool() {
        return ci.getThreadPool();
    }

}

Fragmentで、このContextオブジェクトを使用するインフレータを作成します。

LayoutInflater localInflater = inflater.cloneInContext(new CordovaContext(getActivity(), this));
View v = localInflater.inflate(R.layout.cordovawebview, container, false);
于 2013-09-10T10:10:41.150 に答える
0
@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;
}
于 2013-08-08T15:15:55.843 に答える