2013-03-03 33 views
4

我在創建第一個android應用程序(從pdf「Android.Application.Development.for.For.Dummies」的無聲切換模式)時遇到麻煩,在main.xml文件中插入Imageview時,當我轉到圖形佈局,圖像不顯示自己,並出現此錯誤:「以下類無法找到: -ImageView(更改爲android.widget.ImageView,修復構建路徑,編輯XML)」爲什麼找不到ImageView類?

這是我的XML代碼

<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> 

感謝

+0

您是否嘗試過清潔您的項目? – codeMagic 2013-03-04 02:02:57

+0

在主要活動中導入android.widget。 – Asteriskiiii 2013-03-04 11:31:06

+0

是的,我已經導入android.widget,但沒有區別 – 2013-03-06 11:05:04

回答

6

這是一個問題,我還有R最近聯繫。這對我來說是一種誤導性的錯誤,並且讓我在今天早上難倒了大約30分鐘。這不是說Android無法找到這個類。這是導入圖像源文件時遇到的問題。

現在找不到圖像的原因是因爲我沒有正確地將源圖像複製到目錄中。我已經選擇將源文件從另一個文件夾目錄拖動到適當的位置。在這個過程中,它沒有正確複製文件。或者將其複製爲只是其他資源的鏈接。

因此,Android不知道如何處理資源並拋出奇怪的錯誤。爲了解決這個問題,你需要刪除舊的圖像源,然後用直接完整的副本替換它們的等價物。然後你應該看到這個錯誤消失了,應用程序按照預期從xml佈局代碼中工作。

+1

謝謝,我已經取代了PNG圖像(它包含一個錯誤,奇怪),現在一切都很好 – 2013-03-09 12:55:02

+0

我有同樣的錯誤。它是可繪製的嗎? – dannyroa 2013-05-22 19:29:23

+0

它是可繪製文件夾中的一個.bmp .jpg或.png格式,是的。 – 2013-05-22 19:41:19

0

我曾遇到同樣的問題。爲了解決這個問題,我替換了所有現有的圖像,現在它已經工作了。

+0

應該不是答案。 – Avijit 2013-12-23 07:01:47

0

我有同樣的問題,當我的圖像src在我的xml中添加一個新的ImageView。

我試圖用Photoshop打開這個圖像,並得到一個「模塊格式錯誤」。所以圖像被破壞了。問題解決了創建一個類似於第一個圖像的新圖像。

希望它的作品:)