ViewPager中的InflateException

时间:2016-06-22 13:26:26

标签: android android-fragments android-viewpager

我已经实现了一个包含4个片段的ViewPager。但由于某些原因,当我滑动到第3个片段时它会抛出error

问题是我只在我的模拟器(Lollipop)中运行代码时才会收到此错误,应用程序在我的设备(Kitkat)中正常运行。

片段的XML文件是

> _.isMatch({a: 'A', n: 0}, {a: 'A'})
true

另外两个片段也是相似的,唯一的变化是文本和嵌入的图像。

片段的java文件是

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    android:id="@+id/linearlayout1"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent">
   <!--        android:background="#616161"-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/learn"
        android:textSize="25sp"
        app:layout_marginTopPercent="3%"
        android:textColor="#ffffff"
        android:text="Power of Foresight" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_below="@id/learn"
        app:layout_marginTopPercent="5%"
        android:textSize="20sp"
        android:textColor="#ffffff"
        android:textAlignment="center"
        android:text="With our advanced algorithms we predict the questions that could appear in the next test. "/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView3"
        android:gravity="center"
        android:src="@drawable/no_background2"/>
</android.support.percent.PercentRelativeLayout>
</android.support.percent.PercentRelativeLayout>

ViewPager适配器如下

public class EntryPage1 extends Fragment {
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstances){

    return inflater.inflate(R.layout.entrypage_1,container,false);
   }
}

错误日志如下

public class EntryPageHolder1 extends FragmentActivity{
private PagerAdapter mPagerAdapter;
int[] pageColors = new int[]{Color.parseColor("#3367D6"),Color.parseColor("#00BCD4"),Color.parseColor("#F4B400"),Color.TRANSPARENT};

int currentColor = pageColors[0];
ValueAnimator colorAnimation;
View animatedColorView;
ViewPager pager;
@Override
public void onCreate(Bundle SavedInstances){
    super.onCreate(SavedInstances);
    super.setContentView(R.layout.activity_entrypage_holder);
    animatedColorView=(View)findViewById(R.id.animated_color_view);
    this.initialisepaging();
    View decorView = getWindow().getDecorView();
    // Hide the status bar.
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);
    // Remember that you should never show the action bar if the
    // status bar is hidden, so hide that too if necessary.
    ActionBar actionBar = getActionBar();
    actionBar.hide();
}
public void initialisepaging(){
    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, EntryPage2.class.getName()));
    fragments.add(Fragment.instantiate(this, EntryPage3.class.getName()));
    fragments.add(Fragment.instantiate(this, EntryPage1.class.getName()));
    fragments.add(Fragment.instantiate(this, EntryPage4.class.getName()));
    this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(), fragments);
    pager = (ViewPager)super.findViewById(R.id.viewpager);
    pager.setAdapter(this.mPagerAdapter);
    pager.setPageTransformer(true, new DefaultTransformer());
    animateToColor(pageColors[0]);
    pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            //animateToColor(pageColors[position]);
        }

        @Override
        public void onPageSelected(int position) {
            //Toast.makeText(getApplicationContext(),"Listener executing",Toast.LENGTH_SHORT).show();
            animateToColor(pageColors[position]);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
}
public void animateToColor(int colorTo) {
    if (colorAnimation != null) {
        colorAnimation.cancel();
    }
    colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), currentColor, colorTo);
    colorAnimation.setDuration(150); // milliseconds
    colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

        @Override
        public void onAnimationUpdate(ValueAnimator animator) {
            currentColor = (int) animator.getAnimatedValue();
            animatedColorView.setBackgroundColor(currentColor);
        }

    });
    colorAnimation.start();
}
}

这个错误的原因是什么?为什么它在一个设备上正确运行而在另一个设备上没有?

1 个答案:

答案 0 :(得分:0)

试试这个,

getActivity().getLayoutInflater().inflate(R.layout.entrypage_1,null);