gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:42:06 25 4
gpt4 key购买 nike

我正在尝试实现 CustomViewPager,但它说 Support 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;
}
}

这是我的 Activity :

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>

它基本上落在这一行上 -> 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)

如有任何帮助,我们将不胜感激!

最佳答案

引用来自 Sir Arthur Conan Doyle's The Sign of the Four 的虚构人物夏洛克·福尔摩斯 (Sherlock Holmes) :

How often have I said to you that when you have eliminated the impossible, whatever remains, however improbable, must be the truth?

如果您正在扩充布局,而 Android 返回一个 ViewPager,这很可能是因为布局包含一个 ViewPager,而不是您所期望的(在此案例,一个 CustomViewPager)。拥有多个版本的布局资源会使这变得复杂,因为您正在处理的版本可能没有 ViewPager,但另一个版本有。

如果您熟悉命令行,res/ 目录树的 fgrep,搜索 android。 support.v4.view.ViewPager,将是一种追踪可能仍引用 ViewPager 的布局资源的方法。

关于android.support.v4.view.ViewPager 无法转换为 com.package.CustomViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31724840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com