如何解决UnsupportedOperationException:无法转换为dimension:type = 0x12

时间:2015-04-04 05:43:03

标签: java android eclipse android-layout exception-handling

我正在制作一个应用程序来解释Android Activity Lifecycle但我遇到了android布局的问题。这是我的布局:

    <RelativeLayout 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"
    android:background="@drawable/background"
    tools:context="br.com.fiap.androidactivitylifecycle.MainActivity" >

    <ImageView
        android:id="@+id/imgAndroidRobot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="5dp"
        android:layout_marginStart="5dp"
        android:contentDescription="@string/android_robot"
        android:src="@drawable/android" />

    <ImageView
        android:id="@+id/imgActivityCreated"
        android:layout_width="85dp"
        android:layout_height="35dp"
        android:layout_above="@+id/imgAndroidRobot"
        android:layout_marginStart="23dp"
        android:layout_marginBottom="08dp"
        android:layout_toEndOf="@+id/imgAndroidRobot"
        android:contentDescription="@string/activity_created"
        android:src="@drawable/activity_created" />

    <TextView
        android:id="@+id/txtAndroidOnCreate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imgAndroidRobot"
        android:layout_alignStart="@+id/imgAndroidRobot"
        android:text="@string/on_create" />

    <TextView
        android:id="@+id/txtStartedOnResume"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imgActivityStarted"
        android:layout_marginStart="45dp"
        android:layout_marginBottom="17dp"
        android:layout_toEndOf="@+id/txtAndroidOnCreate"
        android:text="@string/on_resume" />

    <TextView
        android:id="@+id/txtCreatedOnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/imgActivityCreated"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/txtAndroidOnCreate"
        android:text="@string/on_start" />

    <ImageView
        android:id="@+id/imgActivityStarted"
        android:layout_width="85dp"
        android:layout_height="35dp"
        android:layout_above="@+id/txtCreatedOnStart"
        android:layout_toEndOf="@+id/txtCreatedOnStart"
        android:layout_marginStart="8dp"
        android:contentDescription="@string/activity_started"
        android:src="@drawable/activity_started" />

    <ImageView
        android:id="@+id/imgActivityResumed"
        android:layout_width="85dp"
        android:layout_height="35dp"
        android:layout_above="@+id/txtStartedOnResume"
        android:layout_marginStart="45dp"
        android:layout_toEndOf="@+id/imgActivityCreated"
        android:contentDescription="@string/activity_resumed"
        android:src="@drawable/activity_resumed" />

    <TextView
        android:id="@+id/txtPausedOnResume"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imgActivityStarted"
        android:layout_toEndOf="@+id/imgActivityStarted"
        android:layout_marginBottom="11dp"
        android:text="@string/on_resume" />

    <TextView
        android:id="@+id/txtResumedOnPause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imgActivityResumed"
        android:layout_marginStart="17dp"
        android:layout_toEndOf="@+id/txtPausedOnResume"
        android:text="@string/on_pause" />

    <ImageView
        android:id="@+id/imgActivityPaused"
        android:layout_width="85dp"
        android:layout_height="35dp"
        android:layout_above="@+id/txtCreatedOnStart"
        android:layout_toEndOf="@+id/imgActivityResumed"
        android:src="@drawable/activity_paused"
        android:contentDescription="@string/activity_paused" />

    <TextView
        android:id="@+id/txtStoppedOnRestart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/txtAndroidOnCreate"
        android:layout_alignStart="@+id/txtPausedOnResume"
        android:layout_toEndOf="@+id/txtAndroidOnCreate"
        android:text="@string/on_restart" />

    <ImageView
        android:id="@+id/imgActivityDestroyed"
        android:layout_width="55dp"
        android:layout_height="40dp"
        android:layout_alignBottom="@+id/imgAndroidRobot"
        android:layout_alignEnd="@+id/txtStoppedOnDestroy"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:contentDescription="@string/activity_destroyed"
        android:src="@drawable/activity_destroyed" />

    <TextView
        android:id="@+id/txtStoppedOnDestroy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imgAndroidRobot"
        android:layout_alignParentEnd="true"
        android:text="@string/on_destroy" />

    <TextView
        android:id="@+id/txtOnRestartOnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/imgActivityCreated"
        android:layout_below="@+id/txtCreatedOnStart"
        android:layout_marginStart="@+id/txtCreatedOnStart"
        android:layout_marginLeft="85dp"
        android:layout_marginBottom="4dp"
        android:text="@string/on_start" />

    <TextView
        android:id="@+id/txtPausedOnStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/txtOnRestartOnStart"
        android:layout_marginStart="@+id/txtPausedOnResume"
        android:layout_toEndOf="@+id/imgActivityPaused"
        android:text="@string/on_stop" />

    <ImageView
        android:id="@+id/imgActivityStopped"
        android:layout_width="85dp"
        android:layout_height="35dp"
        android:layout_alignEnd="@+id/txtPausedOnStop"

        android:layout_alignTop="@+id/imgActivityCreated"
        android:contentDescription="@string/activity_stopped"
        android:src="@drawable/activity_stopped" />

</RelativeLayout>

这种布局非常不同,每个ImageView都是一个激活生命周期的状态,每个textview都是为实现该状态而执行的方法名称。

这是Android Manifest:

  

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.fiap.androidactivitylifecycle"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="18"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是logcat:

  

04-04 01:17:37.940:D / dalvikvm(462):GC_FOR_ALLOC释放121K,12%释放9614K / 10916K,暂停32ms,总计32ms   04-04 01:17:37.940:I / dalvikvm-heap(462):将堆(frag case)增长到11.314MB,用于815124字节分配   04-04 01:17:37.965:D / dalvikvm(462):GC_FOR_ALLOC释放1K,12%释放10409K / 11716K,暂停24ms,总计24ms   04-04 01:17:37.985:D / dalvikvm(462):GC_FOR_ALLOC释放&lt; 1K,12%free 10409K / 11716K,暂停19ms,总计19ms   04-04 01:17:37.985:I / dalvikvm-heap(462):将堆(frag case)增长到14.421MB,用于3260448字节分配   04-04 01:17:38.015:D / dalvikvm(462):GC_FOR_ALLOC释放&lt; 1K,9%free 13593K / 14904K,暂停27ms,总计27ms   04-04 01:17:38.105:D / AndroidRuntime(462):关闭VM   04-04 01:17:38.105:W / dalvikvm(462):threadid = 1:线程退出未捕获异常(组= 0x419bf700)   04-04 01:17:38.145:E / AndroidRuntime(462):致命异常:主要   04-04 01:17:38.145:E / AndroidRuntime(462):java.lang.RuntimeException:无法启动活动ComponentInfo {br.com.fiap.androidactivitylifecycle / br.com.fiap.androidactivitylifecycle.MainActivity}:java.lang .UnsupportedOperationException:无法转换为dimension:type = 0x12   04-04 01:17:38.145:E / AndroidRuntime(462):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.app.ActivityThread.access $ 700(ActivityThread.java:159)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1316)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.os.Handler.dispatchMessage(Handler.java:99)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.os.Looper.loop(Looper.java:176)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.app.ActivityThread.main(ActivityThread.java:5419)   04-04 01:17:38.145:E / AndroidRuntime(462):at java.lang.reflect.Method.invokeNative(Native Method)   04-04 01:17:38.145:E / AndroidRuntime(462):at java.lang.reflect.Method.invoke(Method.java:525)   04-04 01:17:38.145:E / AndroidRuntime(462):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046)   04-04 01:17:38.145:E / AndroidRuntime(462):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)   04-04 01:17:38.145:E / AndroidRuntime(462):at dalvik.system.NativeStart.main(Native Method)   04-04 01:17:38.145:E / AndroidRuntime(462):引起:java.lang.UnsupportedOperationException:无法转换为维度:type = 0x12   04-04 01:17:38.145:E / AndroidRuntime(462):在android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:464)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.view.ViewGroup $ MarginLayoutParams。(ViewGroup.java:6259)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.widget.RelativeLayout $ LayoutParams。(RelativeLayout.java:1246)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:1083)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:84)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.view.LayoutInflater.rInflate(LayoutInflater.java:768)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.view.LayoutInflater.inflate(LayoutInflater.java:498)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.view.LayoutInflater.inflate(LayoutInflater.java:398)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.view.LayoutInflater.inflate(LayoutInflater.java:354)   04-04 01:17:38.145:E / AndroidRuntime(462):at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.app.Activity.setContentView(Activity.java:1956)   04-04 01:17:38.145:E / AndroidRuntime(462):at br.com.fiap.androidactivitylifecycle.MainActivity.onCreate(MainActivity.java:11)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.app.Activity.performCreate(Activity.java:5372)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)   04-04 01:17:38.145:E / AndroidRuntime(462):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)   04-04 01:17:38.145:E / AndroidRuntime(462):... 11 more

我希望有人可以帮助我。谢谢你。

2 个答案:

答案 0 :(得分:1)

检查以下行的保证金起始值

 android:layout_marginStart="@+id/txtCreatedOnStart"
 android:layout_marginStart="@+id/txtPausedOnResume"

尝试提供dp值

答案 1 :(得分:0)

试试这个,

删除除ids之外的每个小部件属性的“+”符号,例如

android:layout_above="@+id/imgAndroidRobot"

进入android:layout_above="@id/imgAndroidRobot"