java - Android: changing an ImageView source makes my application close unexpectedly -
i signed in order ask help. i'm doing application displays image gallery. when click of images, must open image , show it.
there 2 activities: 'galeria' , 'imagen'. 'galeria' displays image gallery , 'imagen' displays image clicked. when image clicked, create second activity using intent seen in code below.
galeria.java code:
package com.ejemplo.galeria; import android.os.bundle; import android.app.activity; import android.content.intent; import android.view.view; public class galeria extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); } //called when image clicked (i use imagebuttons) public void openimage(view view){ intent = new intent(this,imagen.class); i.putextra("imagen", view.getid()); startactivity(i); } }
the method 'openimage()' called when of imagebutton pushed (whatever button press, same method called). here 'main.xml' code:
<relativelayout 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:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".galeria" > <horizontalscrollview android:layout_width="fill_parent" android:layout_height="fill_parent" android:id = "@+id/scrollview"> <linearlayout android:id="@+id/layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <imagebutton android:id="@+id/imagen1" android:layout_width="100dp" android:layout_height="100dp" android:scaletype="fitxy" android:onclick="openimage" android:src="@drawable/imagen1"></imagebutton> <imagebutton android:id="@+id/imagen2" android:layout_width="100dp" android:layout_height="100dp" android:scaletype="fitxy" android:onclick="openimage" android:src="@drawable/imagen2"></imagebutton> <imagebutton android:id="@+id/imagen3" android:layout_width="100dp" android:layout_height="100dp" android:scaletype="fitxy" android:onclick="openimage" android:src="@drawable/imagen3"></imagebutton> <imagebutton android:id="@+id/imagen4" android:layout_width="100dp" android:layout_height="100dp" android:scaletype="fitxy" android:onclick="openimage" android:src="@drawable/imagen4"></imagebutton> <imagebutton android:id="@+id/imagen5" android:layout_width="100dp" android:layout_height="100dp" android:scaletype="fitxy" android:onclick="openimage" android:src="@drawable/imagen5"></imagebutton> <imagebutton android:id="@+id/imagen6" android:layout_width="100dp" android:layout_height="100dp" android:scaletype="fitxy" android:onclick="openimage" android:src="@drawable/petergriffin"></imagebutton> <imagebutton android:id="@+id/imagen7" android:layout_width="100dp" android:layout_height="100dp" android:scaletype="fitxy" android:onclick="openimage" android:src="@drawable/gato"></imagebutton> </linearlayout> </horizontalscrollview> </relativelayout>
so, code of 'image' class:
package com.ejemplo.galeria; import android.app.activity; import android.content.intent; import android.os.bundle; import android.widget.imageview; public class imagen extends activity{ @override protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); intent = getintent(); int id = i.getintextra("imagen", -1); if(id != -1){ imageview iv = (imageview) findviewbyid(r.id.imagev1); iv.setimageresource(id); //error setcontentview(r.layout.imagen); } } }
and xml code of 'imagen.xml' file:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <imageview android:id="@+id/imagev1" android:layout_height="wrap_content" android:layout_width="wrap_content" ></imageview> </linearlayout>
so, when modify source of imageview, applications closes unexpectedly, , have no idea why. i've been looking found nothing, wish can me, i'm getting mad.
thank you!
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
ok found issue
i changed parameter in method 'setimageresource(id)' r.drawable.imagen1 , constructed switch case statement.
but kept closing unexpectedly!!!
so, in method 'oncreate()' of 'imagen' class, moved line 'setcontentview(r.layout.imagen)' end of method beginning.
problem solved :)
so here:
intent = new intent(this,imagen.class); i.putextra("imagen", view.getid()); startactivity(i);
you put view id in intent, after:
int id = i.getintextra("imagen", -1); if(id != -1){ imageview iv = (imageview) findviewbyid(r.id.imagev1); iv.setimageresource(id); //error setcontentview(r.layout.imagen); }
you try assign r.id
image resource, wrong, because setimageresource
expecting r.drawable.my_image
. please follow javadoc.
Comments
Post a Comment