1

所以,基本上我有一个按钮,当按下它时,它会从它的布局中删除它自己并添加一个带有 ~20 个片段的 ViewPager。

它可以在运行 4.0.4 的三星 Galaxy S II 和运行 4.2.2 的 LG Nexus 4 上正常工作。但是,在运行 2.3.5 的 HTC Desire 或运行 2.3.3 的三星 Galaxy Ace 上尝试时,它会因此异常而崩溃。

FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.ImageView
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:901)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
    at android.view.View.measure(View.java:8553)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:701)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:311)
    at android.view.View.measure(View.java:8553)
    at com.cidaut.blueparking.ui.SlidingDrawer.onMeasure(SlidingDrawer.java:210)
    at android.view.View.measure(View.java:8553)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
at android.view.View.measure(View.java:8553)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
    at android.view.View.measure(View.java:8553)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
    at android.view.View.measure(View.java:8553)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
    at android.view.View.measure(View.java:8553)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8553)
    at android.view.ViewRoot.performTraversals(ViewRoot.java:915)
    at android.view.ViewRoot.handleMessage(ViewRoot.java:1991)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:150)
    at android.app.ActivityThread.main(ActivityThread.java:4385)
at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
    at dalvik.system.NativeStart.main(Native Method)

这不是最具说明性的例外,因为它没有说明问题所在。我什至在整个项目中都没有任何 ImageViews。我不知道怎么了。

这是初始化视图的代码:

private void initCajon() {
        firstLayout = View.inflate(this, R.layout.drawer_item_first, null);
        secondLayout = new ViewPager(this);
        // Stupid hack. Bug de Android, sin esta línea no funciona.
        secondLayout.setId(R.id.abs__home);
        thirdLayout = View.inflate(this, R.layout.drawer_item_third, null);

        bFinalizar = thirdLayout.findViewById(R.id.button1);
        bSaldo = thirdLayout.findViewById(R.id.button2);

        textViewSegundos = (TextView) thirdLayout
                .findViewById(R.id.textViewSegundos);
        textViewMinutos = (TextView) thirdLayout
                .findViewById(R.id.textViewMinutos);
        textViewHoras = (TextView) thirdLayout.findViewById(R.id.textViewHoras);

        textViewSaldo = (TextView) thirdLayout.findViewById(R.id.textViewSaldo);

        firstLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View v) {
                if (LoginScreen.isLogged()) {
                    l = map.getMap().getMyLocation();
                    if (l != null) {
                        initDataPrimerClick();
                    } else {
                        showToast("La localización aun se está estableciendo. Intentalo dentro de unos segundos.");
                    }
                } else {
                    new LoginScreen_.IntentBuilder_(MapScreen.this).start();
                }
            }
        });

        bFinalizar.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View v) {
                showDialog("Finalizando aparcamiento...");
                stop();
            }
        });

        bSaldo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View v) {
                SaldoScreen_.intent(MapScreen.this).start();
            }
        });

        slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {

            @Override
            public void onDrawerOpened() {
                textViewTime.setVisibility(View.GONE);
            }
        });

        slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {

            @Override
            public void onDrawerClosed() {
                if (thirdShown) {
                    textViewTime.setVisibility(View.VISIBLE);
                } else {
                    textViewTime.setVisibility(View.GONE);
                }

            }
        });

        content.removeAllViews();
        content.addView(firstLayout);
        firstLayout.getLayoutParams().width = LayoutParams.MATCH_PARENT;
        textViewTime.setVisibility(View.GONE);
    }

这是执行布局切换的代码:

protected void initViewsPrimerClick(final LatLng posicionReal,
            final int index, final FragmentAdapter startParkingAdapter) {
        final OnPageChangeListener listener = new OnPageChangeListener() {

            @Override
            public void onPageSelected(final int arg0) {
                final StartParkingFragment fragment = (StartParkingFragment) startParkingAdapter
                        .getItem(arg0);
                final Tramo tr = fragment.getTramo();
                final LatLng pos = MapMatch.getPuntoProyectadoEnTramo(
                        posicionReal, tr);
                getSupportActionBar().setTitle(calleTramo.get(tr.getId()));
                pintaMarkerProyectado(pos);
            }

        @Override
        public void onPageScrolled(final int arg0, final float arg1,
                final int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(final int arg0) {

        }
    };

    secondLayout.setOnPageChangeListener(listener);
    secondLayout.setAdapter(startParkingAdapter);
    secondLayout.setCurrentItem(index);
    listener.onPageSelected(index);
    thirdShown = false;
    content.removeAllViews();
    content.addView(secondLayout);
}

我不知道我的代码有什么问题,所以任何见解都会非常好。

以防万一:这些IntentBuilder_对象是由 AndroidAnnotations 自动创建的,并且可以正常工作。

4

0 回答 0