android.support.v4.view.ViewPager无法强制转换为com.package.CustomViewPager

时间:2015-07-30 13:13:48

标签: android casting android-viewpager

我试图实现一个CustomViewPager,但它说支持ViewPager不能被转换为自定义的。我很困惑非CustomViewPager的来源。

(包名取代隐私)

这是我的CustomViewPager

package com.domain.heswresources;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Created by andrewb on 30/07/2015.
 */
public class CustomViewPager extends ViewPager {

    private Boolean enabled;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.enabled = true;

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.enabled) {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    public void setPagingEnabled(Boolean enabled) {
        this.enabled = enabled;
    }
}

这是我的活动:

package com.domain.heswresources;

import android.app.ActionBar;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import com.domain.CustomViewPager;

import com.viewpagerindicator.CirclePageIndicator;


public class TutorialActivity extends FragmentActivity {

    private static final int NUM_PAGES = 4;
    private int pP = 1;
    private CustomViewPager mPager;
    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_screen_slide);

        ActionBar ab = getActionBar();
        ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.dash_action_bar));
        ab.setLogo(getResources().getDrawable(R.drawable.ic_logo));
        ab.setTitle("Getting started");

        getWindow().setBackgroundDrawableResource(R.drawable.main_background);

        mPager = (CustomViewPager) findViewById(R.id.pager);
        mPagerAdapter = new TutorialPagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);


        //Bind the title indicator to the adapter (requires the ViewPagerIndicator library by Jake Wharton)
        CirclePageIndicator pageIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
        pageIndicator.setViewPager(mPager);

        pageIndicator.setOnPageChangeListener(new CustomViewPager.OnPageChangeListener() {
            boolean isReverse = false;

            @Override
            public void onPageSelected(int page) {
                if (String.valueOf(pP).equals(String.valueOf(page))) {
                    isReverse = true;
                } else {
                    isReverse = false;
                }
                switch (page) {
                    case 0:
                        getWindow().setBackgroundDrawableResource(R.drawable.tran1);
                        TransitionDrawable trans1 = (TransitionDrawable) getWindow().getDecorView().getBackground();
                        trans1.startTransition(0);
                        trans1.reverseTransition(500);
                        break;
                    case 1:
                        if (isReverse) {
                            getWindow().setBackgroundDrawableResource(R.drawable.tran2);
                            TransitionDrawable trans2 = (TransitionDrawable) getWindow().getDecorView().getBackground();
                            trans2.startTransition(0);
                            trans2.reverseTransition(500);
                        } else {
                            getWindow().setBackgroundDrawableResource(R.drawable.tran1);
                            TransitionDrawable trans2 = (TransitionDrawable) getWindow().getDecorView().getBackground();
                            trans2.startTransition(500);
                        }
                        break;
                    case 2:
                        if (isReverse) {
                            getWindow().setBackgroundDrawableResource(R.drawable.tran3);
                            TransitionDrawable trans2 = (TransitionDrawable) getWindow().getDecorView().getBackground();
                            trans2.startTransition(0);
                            trans2.reverseTransition(500);
                            Log.v("ANDY2", "page 4 to 3");
                        } else {
                            getWindow().setBackgroundDrawableResource(R.drawable.tran2);
                            TransitionDrawable trans2 = (TransitionDrawable) getWindow().getDecorView().getBackground();
                            trans2.reverseTransition(2000);
                        }
                        break;
                    case 3:
                        if (isReverse) {
                            Log.v("ANDY2", "page 5 to 4");
                        } else {
                            getWindow().setBackgroundDrawableResource(R.drawable.tran3);
                            TransitionDrawable trans2 = (TransitionDrawable) getWindow().getDecorView().getBackground();
                            trans2.reverseTransition(500);
                        }
                        break;
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                pP = arg0;
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    public void OnClick(View v) {
        int id = v.getId();
        if (id == R.id.button1) {
            Editor ed = MainActivity.prefs.edit();
            ed.putString("type", "Acute");
            //ed.putBoolean(MainActivity.SHOW_TUTORIAL, false);
            ed.commit();
            finish();

        }
    }

    public void OnClick2(View v) {
        int id = v.getId();
        if (id == R.id.button2) {
            Editor ed = MainActivity.prefs.edit();
            ed.putString("type", "Mental Health");
            //ed.putBoolean(MainActivity.SHOW_TUTORIAL, false);
            ed.commit();
            finish();
        }
    }

    private class TutorialPagerAdapter extends FragmentPagerAdapter {
        public TutorialPagerAdapter(android.support.v4.app.FragmentManager fm) {
            super(fm);
        }

        @Override
        public TutorialFragment getItem(int position) {

            return TutorialFragment.create(position);
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }
}

我的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

<com.domain.CustomViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.viewpagerindicator.TitlePageIndicator
    android:id="@+id/indicator"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true"
    />

</RelativeLayout>

它基本上落在这条线上 - &gt; mPager = (CustomViewPager) findViewById(R.id.pager);

但我不知道为什么这个变量被设置为CustomViewPager。

日志输出(如果有帮助)

07-30 13:50:05.630  19208-19208/com.domain.heswresources E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.domain.heswresources, PID: 19208
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.domain.heswresources/com.domain.heswresources.TutorialActivity}: java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to com.domain.heswresources.CustomViewPager
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.access$900(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to com.domain.heswresources.CustomViewPager
            at com.domain.heswresources.TutorialActivity.onCreate(TutorialActivity.java:53)
            at android.app.Activity.performCreate(Activity.java:5451)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.access$900(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

Sir Arthur Conan Doyle's The Sign of the Four引用虚构人物Sherlock Holmes:

  

我多久经常告诉你,当你消除了不可能的事物时,无论剩下什么,无论多么不可能,都必须是真理?

如果您要对布局进行充气,并且Android返回ViewPager,那很可能是因为布局包含ViewPager,而不是您期望的内容(在这种情况下,{{1} }})。拥有多个版本的布局资源会使这一点变得复杂,因为您正在处理的版本可能没有CustomViewPager,但另一个版本没有。{/ p>

如果您对命令行感到满意,则ViewPager目录树的 fgrep 会搜索res/,这将是一种跟踪方式下来可能仍然引用android.support.v4.view.ViewPager的布局资源。