0

我试图让这段代码工作:

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {

        View v = null;

        if( 0 == position ) {
            String data = "<html><body>Hi there</body><html>";
            WebView wv = (WebView) findViewById(R.id.webView);
            wv.loadData(data, "text/html", null);
            v = wv;
        }
        else {
            LayoutInflater vi = (LayoutInflater) cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.editor, null);
        }

        collection.addView(v,0);

        return v;
    }

我在这里基本上是在模仿这篇文章来构建一个水平滑动视图:http ://android-developers.blogspot.com/2011/08/horizo​​ntal-view-swiping-with-viewpager.html

使用此代码时发生崩溃。我通过尝试将布局插入 ViewGroup 来收集我没有正确地做事。有什么建议么?

03-20 14:49:55.037: ERROR/AndroidRuntime(25990): FATAL EXCEPTION: main
    java.lang.NullPointerException
    at com.EditorHyde.app.b.a(Unknown Source)
    at android.support.v4.view.ViewPager.a(Unknown Source)
    at android.support.v4.view.ViewPager.a(Unknown Source)
    at android.support.v4.view.ViewPager.c(Unknown Source)
    at android.support.v4.view.ViewPager.onMeasure(Unknown Source)
    at android.view.View.measure(View.java:15518)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:15518)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:15518)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:15518)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
    at android.view.View.measure(View.java:15518)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    at android.view.Choreographer.doFrame(Choreographer.java:532)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    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:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

你需要一个演员来解决你的问题。改变:

collection.addView(v,0);

进入:

((ViewPager)collection).addView(v, 0);
于 2013-03-20T21:36:27.113 回答