Ted pottel
Ted pottel

Reputation: 6993

Android crashes when I try to change an ImageView image

I have a ImageView, and I would like the picture to switch between two pictures each time it is pressed. I commented out all the code and just have the code that changes the image the image view shows. It crashes when I call setImageDrawable.

The code:

ImageView but;
but =(ImageView) findViewById(R.id.butFavrets);  

// it crashes hear
but.setImageDrawable(  Drawable.createFromPath("@drawable/unselected") ); 

The XML file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/backFeetGallery"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/viewimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"               
        android:src="@drawable/background" />         

     <Button
        android:id="@+id/butEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/butLeft"
        android:text="Email" />

    <Button
        android:id="@+id/butLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"    
        android:text="Left" />


    <Button
        android:id="@+id/butRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text=" Right " />

      <ImageView
        android:src="@drawable/unselected" 
        android:id="@+id/butFavrest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/butRight" />

</RelativeLayout>

Upvotes: 0

Views: 212

Answers (1)

Aprian
Aprian

Reputation: 1728

Since you put the image on your drawable folder, then use this getResources().getDrawable(R.drawable.unselected); to load the image, instead of createFromPath.

createFromPath is used to create a drawable from file path name (file from sdcard or phone)

Upvotes: 2

Related Questions