简体   繁体   English

为什么我的应用程序由于ImageView而崩溃?

[英]Why is my app crashing because of the ImageView?

I am trying to get a FloatingButton in a fragment. 我试图在一个片段中获取一个FloatingButton

XML: XML:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Demand"
    android:id="@+id/frag_demand"
    android:layout_gravity="center_horizontal|top"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<ImageView
    android:layout_width="65dp"
    android:layout_height="65dp"
    android:id="@+id/imageButton"
    android:background="@drawable/oval"
    android:elevation="10dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="20dp"
    android:layout_marginRight="15dp"
    android:src="@drawable/ic_menu_add"/>

JAVA: JAVA:

public class DemandFragment extends Fragment {

    MenuItem fav;

    ImageButton floatButton;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_demand, container, false);

        floatButton = (ImageButton) rootView.findViewById(R.id.imageButton);
        floatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "Replace with your own action", Snackbar.LENGTH_LONG);
            }
        });

        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setHasOptionsMenu(true);

    }
}

logcat gives: logcat给出:

java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.ImageButton java.lang.ClassCastException:android.widget.ImageView无法转换为android.widget.ImageButton

Why is this not working? 为什么这不起作用?

help 救命

From the given exception 从给定的异常

java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.ImageButton

It is very clear that you are trying to Cast ImageView into an ImageButton. 很明显,您正在尝试将ImageView投射到ImageButton中。 In your case, in your xml you have defined ImageView with id imageButton 在您的情况下,在xml中定义了ID为imageButton的ImageView

<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:id="@+id/imageButton"
android:background="@drawable/oval"
android:elevation="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="20dp"
android:layout_marginRight="15dp"
android:src="@drawable/ic_menu_add"/>

but in your Fragment code, you are doing this 但是在您的片段代码中,您正在执行此操作

floatButton = (ImageButton) rootView.findViewById(R.id.imageButton);

where floatButton is an ImageButton not ImageView as you defined in your xml and hence the exception. 其中floatButton是一个ImageButton,而不是您在xml中定义的ImageView,因此是异常。

Try changing ImageView to ImageButton in your xml or ImageButton to ImageView in your Fragment Code. 尝试将xml中的ImageView更改为ImageButton或将片段代码中的ImageButton更改为ImageView。 Doing it any way will work 采取任何方式都行得通

Why is it crashing? 为什么会崩溃? ImageViews do not extend from ImageButtons . ImageViews不从ImageButtons扩展。 And your java code expects this to not be the case. 而且您的Java代码希望情况并非如此。

To fix, You have two options. 要解决,您有两个选择。

1 - Since one can always call setOnClickListener on any view, Change your reference of floatButton to a View. 1-由于始终可以在任何视图上调用setOnClickListener ,因此请将您对floatButton的引用更改为一个视图。

View floatButton;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_demand, container, false);

    floatButton = rootView.findViewById(R.id.imageButton);
    floatButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Snackbar.make(v, "Replace with your own action", Snackbar.LENGTH_LONG);
        }
    });

    return rootView;
}

2 - Or, should you actually want an ImageButton, change your xml layout to use an ImageButton (instead of ImageView) and don't change any Java code: 2-或者,如果您实际上想要一个ImageButton,则将XML布局更改为使用ImageButton(而不是ImageView),并且不要更改任何Java代码:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Demand"
    android:id="@+id/frag_demand"
    android:layout_gravity="center_horizontal|top"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<ImageButton
    android:layout_width="65dp"
    android:layout_height="65dp"
    android:id="@+id/imageButton"
    android:background="@drawable/oval"
    android:elevation="10dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="20dp"
    android:layout_marginRight="15dp"
    android:src="@drawable/ic_menu_add"/>

floatButton = (ImageButton) rootView.findViewById(R.id.imageButton); floatButton =(ImageButton)rootView.findViewById(R.id.imageButton);

but in your xml it is an ImageView . 但是在您的xml中是ImageView。 You have to change that to ImageButton in xml or vice versa. 您必须将其更改为xml中的ImageButton,反之亦然。

You can not cast ImageView in your xml to ImageButton. 您不能将xml中的ImageView强制转换为ImageButton。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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