在android中使用ViewFlipper

时间:2014-04-02 06:05:21

标签: android viewflipper

您好我使用以下代码翻转图像以及说明。

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

    <ViewFlipper
        android:id="@+id/view_flipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/lightning" />

            <TextView
                style="@style/ImageTitle"
                android:text="@string/lightning" />
        </RelativeLayout>

       <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

         <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/color_baloons" />

            <TextView
                style="@style/ImageTitle"
                android:text="@string/color_baloons" />
        </RelativeLayout> 

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                android:src="@drawable/natural_wall" />

            <TextView`enter code here`
                style="@style/ImageTitle"
                android:text="@string/natural_wall" />
        </RelativeLayout>
   </ViewFlipper>
</RelativeLayout>

如果我运行上面的代码,应用程序崩溃了。但是,如果我从第二个RelativeLayout中删除ImageView,代码工作正常。我也在这里粘贴logcat。

    04-02 01:58:54.683: E/AndroidRuntime(1166): FATAL EXCEPTION: main
    04-02 01:58:54.683: E/AndroidRuntime(1166): java.lang.RuntimeException: Unable to start             activityComponentInfo{com.javatechig.viewflipper/com.javatechig.viewflipper.ViewFlipperSampleActivity}: android.view.InflateException: Binary XML file line #34: Error inflating class <unknown>
    04-02 01:58:54.683: E/AndroidRuntime(1166):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
    04-02 01:58:54.683: E/AndroidRuntime(1166):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
    04-02 01:58:54.683: E/AndroidRuntime(1166):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    04-02 01:58:54.683: E/AndroidRuntime(1166):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
    04-02 01:58:54.683: E/AndroidRuntime(1166):     at android.os.Handler.dispatchMessage(Handler.java:99)
    04-02 01:58:54.683: E/AndroidRuntime(1166):     at android.os.Looper.loop(Looper.java:137)
    04-02 01:58:54.683: E/AndroidRuntime(1166):     at android.app.ActivityThread.main(ActivityThread.java:5103)
    04-02 01:58:54.683: E/AndroidRuntime(1166):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-02 01:58:54.683: E/AndroidRuntime(1166):     at java.lang.reflect.Method.invoke(Method.java:525)
    04-02 01:58:54.683: E/AndroidRuntime(1166):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    04-02 01:58:54.683: E/AndroidRuntime(1166):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    04-02 01:58:54.683: E/AndroidRuntime(1166):     at dalvik.system.NativeStart.main(Native Method)
    04-02 01:58:54.683: E/AndroidRuntime(1166): Caused by: android.view.InflateException: Binary XML file line #34: Error inflating class <unknown>

相同的代码适用于设备类型3.7,当运行int 5.4时它不起作用。 minSdkVersion是18。

谢谢

2 个答案:

答案 0 :(得分:0)

好吧我认为问题出在你的第二个RelativeLayout

 <RelativeLayout
   android:layout_width="fill_parent"
     android:layout_height="fill_parent" 
     android:src="@drawable/color_baloons" >

删除

  

机器人:SRC =&#34; @可绘制/ color_baloons&#34;

并像在其他人那样尝试

   <RelativeLayout
       android:layout_width="fill_parent"
         android:layout_height="fill_parent"   >

答案 1 :(得分:0)

我必须在创建模拟器时增加VM堆。它默认为16,我尝试使用64并且它有效。