【问题标题】:android.view.View cannot be cast to com.zhangxaochen.mysensor.MyViewandroid.view.View 无法转换为 com.zhangxaochen.mysensor.MyView
【发布时间】:2012-12-31 06:05:02
【问题描述】:

我正在尝试在我的 xml 文件中添加自定义视图,代码如下:

    <View
    android:id="@+id/myView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/radioGroupSensorRate"
    android:layout_centerHorizontal="true"
    class="com.zhangxaochen.mysensor.MyView" />

但随后出现运行时异常:

12-31 12:29:20.033: E/AndroidRuntime(4432): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zhangxaochen.mysensor/com.zhangxaochen.mysensor.MyGravitySensor}: java.lang.ClassCastException: android.view.View cannot be cast to com.zhangxaochen.mysensor.MyView

类 MyView 已经在我的 java 文件中定义了,我知道 &lt;com.packageName.ClassName attrs... /&gt; 方式确实可以添加自定义小部件,但我听到有人说第二种方式就像上面的 &lt;View class='com.zhangxaochen.mysensor.MyView' .../&gt; 一样,我尝试了然后失败的。谁能告诉我如何解决这个问题?还是那句话是假的?

【问题讨论】:

    标签: android custom-controls


    【解决方案1】:

    解决了~

    第二种方式:&lt;View class="packagename.ClassName" .../&gt;

    不起作用只是因为标签'View'被大写,它应该是小写的:'view'......

    【讨论】:

      【解决方案2】:

      Xml 中创建您的自定义视图作为关注

       <com.zhangxaochen.mysensor.MyView
          android:id="@+id/myView"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_below="@+id/radioGroupSensorRate"
          android:layout_centerHorizontal="true"
          />
      

      【讨论】:

      • thx,但也许你没有读完我的所有文字,我已经尝试过你提到的方式,并且确实有效,我只是想知道是否有第二种方式
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多