问题
Hi I am developing application .My application run perfectly on all devices.But when we run this application on mdpi phone its getting me this error:
Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12.
Kindly help me where i am wrong.
This is My Code:-
<RelativeLayout
android:id="@+id/LevLayId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="140dp" >
<TextView
android:id="@+id/totlScorId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/score_timer"
android:gravity="center"
android:textColor="#ffffff" />
<TextView
android:id="@+id/levelId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/score_timer"
android:gravity="center"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/LayAboButId"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/option_last_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="7px"
android:orientation="horizontal" >
<Button
android:id="@+id/opt3Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_normal_new"
android:gravity="center|center_vertical"
android:textColor="#ffffff"
android:textSize="11dp" />
<Button
android:id="@+id/opt4Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_normal"
android:gravity="center|center_vertical"
android:textColor="#ffffff"
android:textSize="11dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/option_pre_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/option_last_row"
android:layout_marginBottom="3px"
android:orientation="horizontal" >
<Button
android:id="@+id/opt1Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_normal_new"
android:gravity="center|center_vertical"
android:textColor="#ffffff"
android:textSize="11dp" />
<Button
android:id="@+id/opt2Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_normal"
android:gravity="center|center_vertical"
android:textColor="#ffffff"
android:textSize="11dp" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/imgLayId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginRight="@id/totlScorId"
android:layout_marginTop="70dp" >
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ImageView
android:id="@+id/imgViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ImageView
android:id="@+id/imgViewframeId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/photo_frame" />
</RelativeLayout>
回答1:
Use one Parent layout and Use below XML code instead of your code, it will solve your problem.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/LevLayId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="140dp" >
<TextView
android:id="@+id/totlScorId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/score_timer"
android:gravity="center"
android:textColor="#ffffff" />
<TextView
android:id="@+id/levelId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/score_timer"
android:gravity="center"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/LayAboButId"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/option_last_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="7px"
android:orientation="horizontal" >
<Button
android:id="@+id/opt3Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_normal_new"
android:gravity="center|center_vertical"
android:textColor="#ffffff"
android:textSize="11dp" />
<Button
android:id="@+id/opt4Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_normal"
android:gravity="center|center_vertical"
android:textColor="#ffffff"
android:textSize="11dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/option_pre_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/option_last_row"
android:layout_marginBottom="3px"
android:orientation="horizontal" >
<Button
android:id="@+id/opt1Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_normal_new"
android:gravity="center|center_vertical"
android:textColor="#ffffff"
android:textSize="11dp" />
<Button
android:id="@+id/opt2Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_normal"
android:gravity="center|center_vertical"
android:textColor="#ffffff"
android:textSize="11dp" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/imgLayId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginRight="@id/totlScorId"
android:layout_marginTop="70dp" >
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ImageView
android:id="@+id/imgViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ImageView
android:id="@+id/imgViewframeId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/photo_frame" />
</RelativeLayout>
</RelativeLayout>
回答2:
Just started getting this today, so spotted the change which led to it. In my case, it was because I've had to create a new layout directory layout-xhdpi for a new device. Now, the new device thinks it's "large" (ie. it was using the layouts in layout-large); but it's really xhdpi.
So in the new layout xml, file I added reference to a value from a values-large/dimens.xml
However, this doesn't work.
Creating values-xhdpi/dimens.xml with the referenced value in it, solved the problem.
TL;DR make sure that you have a values-XXXXX directories for each layout-XXXXX directory, and that all relevant values are in there. I believe that values applicable to all sizes will be found in values/dimens.xml, but that's not how my app is structured, so I can't prove it.
来源:https://stackoverflow.com/questions/13626102/caused-by-java-lang-unsupportedoperationexception-cant-convert-to-dimension