【问题标题】:Android Layout switching id's at runtimeAndroid Layout 在运行时切换 id
【发布时间】:2014-01-20 10:37:46
【问题描述】:

在我的项目中,我正在使用卡片库,但发生了一些非常奇怪的事情......

我有一张自定义卡片,它使用带有 CardThumbnailView 和 TextView 的自定义布局,

这是布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_marginLeft="3dp"
              android:gravity="fill_vertical"
              android:layout_height="96dp">

    <it.gmariotti.cardslib.library.view.component.CardThumbnailView
                style="@style/card_thumbnail_outer_layout"
                android:id="@+id/card_exrc_thumb"
                android:layout_width="wrap_content"
                android:layout_gravity="right"
                android:layout_height="match_parent"
                android:layout_margin="3dp"/>

    <LinearLayout
            android:orientation="vertical"
            android:gravity="right"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
            android:id="@+id/card_exrc_title"
            style="@style/exrc_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</LinearLayout>

如您所见,缩略图视图的 id 为“card_exrc_thumb”,文本视图的 id 为“card_exrc_title”。

这就是我在卡片类中使用它们的方式:

protected TextView mExrcTitle;
protected CardThumbnailView mThumb;

public void setupInnerViewElements(ViewGroup parent, View view) {

        // Retrieve elements

        mThumb = (CardThumbnailView) parent.findViewById(R.id.card_exrc_thumb);
        mExrcTitle = (TextView) parent.findViewById(R.id.card_exrc_title);

        // Set Thumb
        CardThumbnail cardThumbail = new CardThumbnail(cContext);
        cardThumbail.setDrawableResource(R.drawable.ic_launcher);
        mThumb.addCardThumbnail(cardThumbail);

        // Set Title
        mExrcTitle.setText(exCurrent.sName);
    }

我得到的异常是非法转换异常 - 就像我试图将 textview 转换为 cardthumbnailview 一样,反之亦然...

java.lang.ClassCastException: android.widget.TextView cannot be cast to it.gmariotti.cardslib.library.view.component.CardThumbnailView
01-02 23:19:26.281: E/AndroidRuntime(781):  at gym.ExerciseCard.setupInnerViewElements(ExerciseCard.java:48)

任何帮助将不胜感激。

【问题讨论】:

  • 你可以为你的自定义元素添加代码吗?
  • 有没有可能上面的XML不是实际膨胀的XML?也许'card_exrc_thumb'作为TextView存在于另一个视图中?进行文件搜索并验证。另外,尝试删除演员表并将类型设置为“视图”,然后查看视图是否是您所期望的。

标签: android exception layout user-interface views


【解决方案1】:

项目...清理...所有项目。如果这不起作用,请手动删除 /bin 和 /gen 目录并重新启动 Eclipse。

【讨论】:

  • 这看起来像是运行时异常,而不是 Eclipse 问题。
  • @NathanielWaggoner 我不确定这是否会解决此特定情况下的问题,因此,就我个人而言,我会发表评论。但是,有时ClassCastExceptions 可能会在运行时发生,因为 Eclipse 无法识别 xml 中的更改。因此,如果是这种情况,清理和重建会有所帮助。
  • 我以前没有遇到过这个问题,但我会买它。 Eclipse 真是一团糟。
  • 我确实已经从我的回答中的步骤中看到了这个确切的问题,并且我有 80% 的把握这个特定的实例将被我的回答解决,所以我觉得它作为一个更好的回答。
猜你喜欢
  • 2011-11-24
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多