Why the ImageView class is not found?

2019-03-30 15:38发布

问题:

i'm having troubles with creating the first android app (Silent Toggle Mode from the pdf "Android.Application.Development.for.For.Dummies" ) with inserting an Imageview in the main.xml file, when I go to the graphical layout, the image isn't showing itself and this error appears:"The following classes could not be found: - ImageView (Change to android.widget.ImageView, Fix Build Path, Edit XML)"

this is my xml code

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:contentDescription="@string/hello_world"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <EditText android:id="@+id/edit_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />

<ImageView 
    android:id="@+id/phone_icon"
    android:contentDescription="@string/description"
    android:layout_width="wrap_content"
    android:scaleType="fitXY"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/phone_on" />

<Button
    android:id="@+id/toggleButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ToggleSilentMode" />

</LinearLayout>

thanks

回答1:

This is one issue that I have also run across recently. It was sort of a misleading error to me and kept me stumped for around 30 minutes this morning. It isn't that Android cannot find the class. It is that it is having trouble importing the image source file.

Now the reason that it is having trouble finding the image is because I did not copy the source image into the directory correctly. I have chosen to drag the source file from another folder directory into the appropriate locations. In the process, it did not copy the file over properly. Or it copied it over as just a link to the other resource.

As a result Android does not know what to do with the resource and throws that odd error. In order to resolve this problem, you need to delete the old image sources and then replace them with direct full copies of their equivalents. Then you should see this error go away and the application working as expected from the xml layout code.



回答2:

I faced the same problem once. To get it out, I replaced all the existing images, now it worked.



回答3:

I had the same issue, when added a new ImageView in my xml with an image src.

I tried to open this image with Photoshop and get an "module format error". So the image was corrupted. The problem was solved creating a new image similar to the first one.

Hope it works :)