【问题标题】:android.widget.imagebutton cannot be cast to android.widget.button Messageandroid.widget.imagebutton 无法转换为 android.widget.button 消息
【发布时间】:2014-03-15 14:04:30
【问题描述】:

您好,我收到此消息 android.widget.imagebutton 无法转换为 android.widget.button(有关 logcat 错误,请参见附件图片),我不明白为什么?因为我的代码似乎是正确的。

代码:

ImageButton Calendar = (ImageButton) findViewById (R.id.Calendar);
Calendar.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent OpenCalendarbtn = new Intent (Menu.this, DisplayCalendar.class);
        startActivity (OpenCalendarbtn);
    }
});

完整的布局xml文件:

`

<ImageButton
    android:id="@+id/Read"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/Write"
    android:layout_marginRight="31dp"
    android:src="@drawable/read" />

<ImageView
    android:id="@+id/AppLogo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/logo" />

<ImageButton
    android:id="@+id/Write"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/AppLogo"
    android:layout_marginLeft="35dp"
    android:layout_marginTop="14dp"
    android:src="@drawable/write" />

<ImageButton
    android:id="@+id/Calendar"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignLeft="@+id/Write"
    android:layout_below="@+id/Write"
    android:layout_marginTop="20dp"
    android:src="@drawable/calendar/>

`

【问题讨论】:

  • R.id.Calendar 是 ImageButton 还是 Button?
  • 请添加您的 layout.xml
  • R.id.Calendar 是一个 ImageButton。

标签: android button compiler-errors android-activity classcastexception


【解决方案1】:

确保您在布局资源文件中使用了&lt;ImageButton&gt; 标签而不是&lt;Button&gt; 标签。 这可能是错误的原因。

【讨论】:

  • 布局资源文件看起来也不错
  • 发布您的完整布局 xml
  • 添加了完整的布局 xml
【解决方案2】:

改变你的

ImageButton Calendar = (ImageButton) findViewById (R.id.Calendar); 

收件人:

Button Calendar = (Button) findViewById (R.id.Calendar); 

【讨论】:

    【解决方案3】:

    这是一个不在您的代码中的 Eclipse 问题。 解决方案是通过 Ctrl+c 将您的代码复制到 xml 并保存 xml 然后再次粘贴,然后保存并运行.....这对我有用。

    【讨论】:

      猜你喜欢
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      • 2017-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多