错误:膨胀类片段

时间:2017-04-27 10:59:44

标签: java android-studio fragment

我试图在片段中制作幻灯片,但是当我尝试在java代码中定义一个视图时,我有这个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.nir.nestleapp, PID: 25042
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nir.nestleapp/com.example.nir.nestleapp.MainPageActivity}: android.view.InflateException: Binary XML file line #37: Binary XML file line #37: Error inflating class fragment

这是java代码:

public class SlideshowFragment extends Fragment {
    Animation Fade_in,Fade_out;
    ViewFlipper viewFlipper;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment_slideshow,container,false);
        viewFlipper=(ViewFlipper)getView().findViewById(R.id.backgroundflipper);
        Fade_in= AnimationUtils.loadAnimation(getActivity(),android.R.anim.fade_in);
        Fade_out=AnimationUtils.loadAnimation(getActivity(),android.R.anim.fade_out);

        viewFlipper.setInAnimation(Fade_in);
        viewFlipper.setInAnimation(Fade_out);

        viewFlipper.setAutoStart(true);
        viewFlipper.setFlipInterval(5000);
        viewFlipper.startFlipping();


        return v;
    }

}

这里是片段xml代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.fragment.NavigationDrawerFragment"
    tools:context="com.example.nir.nestleapp.SlideshowFragment">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/SlideshowFragmentpage"></RelativeLayout>
    <ViewFlipper
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/backgroundflipper">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/ShopPic1"
            android:scaleType="centerCrop"
            android:src="@drawable/nestleshop1"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/ShopPic2"
            android:scaleType="centerCrop"
            android:src="@drawable/nestleshop2"
            />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/ShopPic3"
            android:scaleType="centerCrop"
            android:src="@drawable/nestleshop3"
            />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/ShopPic4"
            android:scaleType="centerCrop"
            android:src="@drawable/nestleshop4"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/ShopPic5"
            android:scaleType="centerCrop"
            android:src="@drawable/nestleshop5"/>
    </ViewFlipper>

</FrameLayout>

我尝试了很多解决方案,但似乎没有任何效果。

更新: 我的活动xml:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main_page"

    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="ברוך הבא"
        android:layout_width="93dp"
        android:layout_height="35dp"
        android:layout_x="75dp"
        android:layout_y="15dp"
        android:id="@+id/TextView10" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="33dp"
        android:layout_x="10dp"
        android:layout_y="15dp"
        android:id="@+id/ConnectTv"
        android:textStyle="bold"
        />

    <TextView
        android:text="TextView"
        android:layout_width="82dp"
        android:layout_height="31dp"
        android:layout_x="30dp"
        android:layout_y="15dp"
        android:id="@+id/textView2"></TextView>

    <fragment
        class="com.example.nir.nestleapp.SlideshowFragment"
        android:id="@+id/SlideshowFrag"
        android:layout_width="match_parent"
        android:layout_height="206dp"
        android:layout_y="55dp"
        tools:layout="@layout/fragment_maps"
        android:layout_x="-4dp">
    </fragment>

    <Button
        android:text="נווט"
        android:layout_width="match_parent"
        android:layout_height="62dp"
        android:layout_x="0dp"
        android:layout_y="452dp"
        android:id="@+id/Waze" />

    <fragment
        class="layout.MapsFragment"
        android:id="@+id/LowerFrag"
        android:layout_width="386dp"
        android:layout_height="193dp"
        android:layout_y="262dp"
        tools:layout="@layout/fragment_maps"
        android:layout_x="-4dp">
    </fragment>

</AbsoluteLayout>

2 个答案:

答案 0 :(得分:1)

取代此代码

viewFlipper=(ViewFlipper)getView().findViewById(R.id.backgroundflipper);

使用此代码:

viewFlipper=(ViewFlipper)v.findViewById(R.id.backgroundflipper);

答案 1 :(得分:0)

在布局中添加片段的基本方法如下所示。你必须提到片段的类名。

<fragment android:name="com.example.nir.nestleapp.SlideshowFragment"
          android:id="@+id/headlines_fragment"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent" />