[英]AppCompatImageView cannot be cast to my class
我的Android Java代碼有問題,希望您能幫幫我。
我的班級有ImageView父級...有些像這樣:
public abstract class BaseStatus extends ImageView
{
public BaseStatus(Context context){
super(context);
}
}
public class BluetoothStatus extends BaseStatus
{
public BluetoothStatus(Context context){
super(context);
}
}
我想從XML獲取組件作為ImageView,您知道...
BluetoothStatus btStatus;
btStatus = (BluetoothStatus) findViewById(R.id.bt_status); // ERROR
XML:
<ImageView
android:layout_width="85dp"
android:layout_height="wrap_content"
android:id="@+id/bt_status"
android:src="@drawable/bluetooth"
android:layout_alignTop="@+id/button3"
android:layout_alignEnd="@+id/lb_laser" />
而且我收到此消息:
引起原因:java.lang.ClassCastException:android.support.v7.widget.AppCompatImageView無法轉換為net.antiradary.radarhunter.activity.status.BluetoothStatus
為什么我無法將ImageView投射到BluetoothStatus? 謝謝您的解答!
這樣做就足夠了...
<net.antiradary.radarhunter.activity.status.BluetoothStatus
android:layout_width="85dp"
android:layout_height="wrap_content"
android:id="@+id/iv_bluetooth"
android:src="@drawable/bluetooth"
android:layout_alignTop="@+id/button3"
android:layout_alignEnd="@+id/lb_laser" />
在我的課上讓這樣的構造函數
class BluetoothStatus extends ImageView
{
public BluetoothStatus(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
我知道我參加晚會很晚,但是以防萬一有人為同樣的問題而努力,當我嘗試在尋呼機適配器圖像中添加縮放功能時,這是一種對我有用的解決方案。
信息:這不是確切的答案,但是對於那些面臨相同問題的人來說,這可能是一個解決方案。 我發布此消息是因為我沒有在stackOverflow的任何地方提供這種解決方案
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/head_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/head_image" />
</RelativeLayout>
在上面的代碼中,只需將<ImageView
替換為<com.yourPackage.TouchImageView
。
TouchImageView (或其他任何類)是一個自定義類(您需要制作),應擴展android.support.v7.widget.AppCompatImageView
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.