2016-05-14 80 views
0

我正在做Android的第一個實驗,我對這個簡單的應用程序有以下問題。爲什麼我無法將ImageView的immage替換爲這個簡單的Android應用程序?

基本上我的應用包括爲ImageView的表示背景IMMAGE,一個TextView的示出的消息和一個按鈕。

當用戶單擊此按鈕時,我的TextView文本必須更改,我的* ImageView的背景圖像也必須更改。

所以這是包含我的主要活動的佈局我activiy_main.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#B388FF" 
    android:orientation="vertical" 
    tools:context=".MainActivity"> 

    <ImageView 
     android:id="@+id/android_cookie_image_view" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:scaleType="centerCrop" 
     android:src="@drawable/before_cookie" /> 

    <TextView 
     android:id="@+id/status_text_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginTop="16dp" 
     android:text="I'm so hungry" 
     android:textColor="@android:color/white" 
     android:textSize="34sp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:text="EAT COOKIE" 
     android:onClick="eatCookie"/> 

</LinearLayout> 

而這是以前的活動代碼:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    public void eatCookie(View v) { 

     TextView messaggio = (TextView) findViewById(R.id.status_text_view); 
     messaggio.setText("I'm so full"); 

     ImageView sfondo = (ImageView) findViewById(R.id.android_cookie_image_view); 
     sfondo.setImageDrawable(Drawable.createFromPath("@drawable/after_cookie")); 

    } 
} 

正如你所看到的,當用戶點擊該按鈕時,它執行的方法是首先檢索TextView參考並更改t分機的這個TextView。它工作正常。

然後獲取相關的ImageView參考,並嘗試改變觀看IMMAGE,我已經被這條線做了:

sfondo.setImageDrawable(Drawable.createFromPath("@drawable/after_cookie")); 

在我的項目我已經把after_cookie.jpg文件放入/res/drawable/文件夾中。

問題是它無法工作。 android_cookie_image_view的默認immage消失,但不會被替換爲after_cookie.jpg圖像。

出了什麼問題?我錯過了什麼?我該如何解決這個問題?

+0

你已經呼籲'onCreate'方法'eatCookie function'? –

+0

將'setImageDrawable()'調用改爲'setImageResource(R.drawable.after_cookie)'。 –

+0

爲什麼在地球上使用[drawable](http://developer.android.com/reference/android/graphics/drawable/Drawable.html#createFromPath(java.lang.String))像這樣(我不確定'@ drawable/something'返回String路徑/ URI)? 'setImageResource(R.drawable)'它有什麼問題,它很容易,你不轉換任何東西只是傳遞'int'值。 – Hosseini

回答

0

改爲使用它。

ImageView sfondo = (ImageView) findViewById(R.id.android_cookie_image_view); 
sfondo.setImageDrawable(getResources().getDrawable(R.drawable.after_cookie)); 

並確保您致電onCreateeatCookie功能。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     eatCookie(); 
    } 
+0

只有當「上下文」可用。 – Hosseini

+0

@Hosseini他正在使用'AppCompatActivity'而不是'Fragment'。不需要添加'View'作爲參數。 –

0

試試這個

sfondo.setImageDrawable(getResources().getDrawable(R.drawable.after_cookie)); 
相關問題