2017-04-27 135 views
0

我试图做一个片段中的幻灯片,但是当我试图定义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> 

我已经尝试了许多解决方案,但似乎没有任何工作。

UPDATE: 我活动的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> 
+0

您的活动xml?\ –

+0

看我的更新 –

回答

1

在这段代码

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

使用该代码的地方:

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

谢谢!现在完美运行 –

+0

好!你可以标记接受的答案。 :) – Sac

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" />