android - Why on the click of "choose image" button, after choosing a image application crashes? -


i have parent class mainactivity calls on click of button , after when click on choose image button, there starts new intent allows me select particular image gallery if same application crashes. also, have override onactivityresult method.

check..

add.java

package com.apna.mycontacts; import java.io.bytearrayoutputstream;  import android.app.activity; import android.content.intent; import android.database.cursor; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.bitmap.compressformat; import android.graphics.drawable.bitmapdrawable; import android.graphics.drawable.drawable; import android.net.uri; import android.os.bundle; import android.provider.mediastore; import android.util.base64; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.imageview; import android.widget.textview;  public  class add extends activity  implements onclicklistener{ edittext fname,lname; button save,pic; string picturepath,str; imageview iv; private static final int selected_picture = 1; @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_add);         pic=(button) findviewbyid(r.id.button2);         fname=(edittext) findviewbyid(r.id.edittext1);     lname=(edittext) findviewbyid(r.id.edittext2);     save=(button) findviewbyid(r.id.button1);      iv=(imageview) findviewbyid(r.drawable.ic_launcher);      save.setonclicklistener(this);     pic.setonclicklistener(this);  }     protected void onactivityresult(int requestcode, int resultcode, intent data) {         // todo auto-generated method stub         super.onactivityresult(requestcode, resultcode, data);     switch(requestcode)     {     case selected_picture:          if(resultcode==result_ok)         {         uri selectedimage=data.getdata();         string[] filename={ mediastore.images.media.data };         cursor cursor=getcontentresolver().query(selectedimage, filename, null,null, null);         cursor.movetofirst();         int columnindex=cursor.getcolumnindex(filename[0]);          picturepath= cursor.getstring(columnindex);          cursor.close();  bitmap yourselectedimage=bitmapfactory.decodefile(picturepath);         drawable d=new bitmapdrawable(yourselectedimage);         iv.setbackground(d);         log.e("abc","error ");         }     }     }     @override     public void onclick(view v) {         // todo auto-generated method stub         switch(v.getid())         {         case r.id.button1:         intent i=new intent();         i.putextra("fname",fname.gettext().tostring());         i.putextra("lname",lname.gettext().tostring());         i.putextra("image",picturepath);         setresult(5,i);         finish();            break;         case r.id.button2:                     intent l=new intent(intent.action_pick,android.provider.mediastore.images.media.external_content_uri);                     startactivityforresult(l,selected_picture);                              break;          }     } } 

and

onactivityresult....


@override     protected void onactivityresult(int requestcode, int resultcode, intent data) {         // todo auto-generated method stub         super.onactivityresult(requestcode, resultcode, data);     if(requestcode==5)     {         string a=data.getstringextra("fname");         string b=data.getstringextra("lname");         string c=data.getstringextra("image");         tv.settext(c);         first.add(a);         last.add(b);         cd.notifydatasetchanged();      }     } 

please me..

that error seems telling iv not defined when call iv.setbackground(d). because calling findviewbyid drawable instead of id. have element in layout mean set background of. need using element's id.


Comments