Android Studio 3.0.1新建应用报错:Can't convert to dimension: type=0x1
Hey there, that error usually pops up when your ConstraintLayout has invalid attribute values the system can’t interpret as valid dimensions. Let’s work through this together:
Common Causes & Step-by-Step Fixes
1. Fix Truncated/Misconfigured ConstraintLayout Attributes
Your XML code got cut off, but the most likely culprit is a messed-up ConstraintLayout-specific property. For example:
- If you wrote
app:layout_constraintWidth_default="wrap"(invalid), replace it withapp:layout_constraintWidth_default="wrap_content"(correct) - If you set
layout_constraintHeight_defaultto an unrecognized value, switch it to one of the valid options:"wrap_content","match_parent", or"percent"(only if paired withlayout_constraintHeight_percentfor percentage sizing)
2. Verify Dimension Resources & Values
Double-check all size-related properties (margins, padding, width/height) for valid formatting:
- Don’t use
@string/resources where@dimen/is required (e.g.,android:margin="@string/margin_16"is wrong; use@dimen/margin_16instead) - Always include units for hardcoded values (e.g.,
16dpinstead of just16)
3. Reference a Valid ConstraintLayout Example
Here’s a complete, error-free ConstraintLayout snippet to use as a guide:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="0dp" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_margin="16dp"/> </android.support.constraint.ConstraintLayout>
4. Update ConstraintLayout Version (Optional but Recommended)
Android Studio 3.0.1 works best with ConstraintLayout 1.1.x. Open your app-level build.gradle and ensure you’re using a stable version:
dependencies { implementation 'com.android.support.constraint:constraint-layout:1.1.3' }
After making these changes, clean and rebuild your project (Build > Clean Project then Build > Rebuild Project) to apply fixes.
内容的提问来源于stack exchange,提问作者Ankon

