简体   繁体   中英

Why am I getting ClassNotFoundException when I define <shape> in layout XML?

I am trying to draw a rectangle by including this in /layout/myactivity.xml :

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
  <corners
      android:radius="2dp"
      android:topRightRadius="0dp"
      android:bottomRightRadius="0dp"
      android:bottomLeftRadius="0dp" />
  <stroke
      android:width="1dp"
      android:color="@android:color/white" />
</shape>

When I try to run the project I get:

05-12 23:37:14.090: E/AndroidRuntime(541): Caused by: java.lang.ClassNotFoundException: android.view.shape
05-12 23:37:14.090: E/AndroidRuntime(541):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
05-12 23:37:14.090: E/AndroidRuntime(541):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-12 23:37:14.090: E/AndroidRuntime(541):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-12 23:37:14.090: E/AndroidRuntime(541):  at android.view.LayoutInflater.createView(LayoutInflater.java:552)
05-12 23:37:14.090: E/AndroidRuntime(541):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:636)
05-12 23:37:14.090: E/AndroidRuntime(541):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
05-12 23:37:14.090: E/AndroidRuntime(541):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
05-12 23:37:14.090: E/AndroidRuntime(541):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
05-12 23:37:14.090: E/AndroidRuntime(541):  ... 22 more

Am I missing something?

Your XML code is fine, the location is the problem. Instead of having this file in your res/layout folder, you will want to save it in res/drawable or a specific drawable folder like res/drawable-xhdpi .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM