android - NullPointerException Error while using AsyncTask in onActivityResult -


i want select image gallery , crop it,then upload image server.if upload success,change view,if upload fail,them give message;

i use onactivityresult manage this,and use asynctask make http post, dismiss progressdialog,and save image,then ran error

java.lang.runtimeexception: failure delivering result resultinfo... 

and caused by

caused by: java.lang.nullpointerexception 

in asynctask codes;

here button crop

avatarbutton.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             intent intent = new intent(intent.action_pick);             intent.settype("image/*");             startactivityforresult(intent,1);         }     }); 

and onactivityresult code

@override protected void onactivityresult(int requestcode, int resultcode, intent data) {      switch (requestcode){         case 1:                           if (data != null) {                     uri uri = data.getdata();                 crop(uri);             }         case 2:              if (data != null) {                     final bitmap bitmap = data.getparcelableextra("data");                     new uploadavatar(userprofileactivity.this, bitmap).execute();                 }        }     super.onactivityresult(requestcode, resultcode, data); } 

crop method

private void crop(uri uri) {     intent intent = new intent("com.android.camera.action.crop");     intent.setdataandtype(uri, "image/*");     intent.putextra("crop", "true");      intent.putextra("aspectx", 1);     intent.putextra("aspecty", 1);      intent.putextra("outputx", 100);     intent.putextra("outputy", 100);      intent.putextra("outputformat", "png");     intent.putextra("nofacedetection", true);     intent.putextra("return-data", true);      startactivityforresult(intent, 2); } 

and asynctask class

class uploadavatar extends asynctask<void,integer,boolean> { private context context; private string avatarstring; private progressdialog progressdialog; private imageview avatarimage; private bitmap avatar;  public uploadavatar(context contextin,bitmap bitmap){      this.context = contextin;     this.avatar = bitmap;     view rootview = ((activity)contextin).getwindow().getdecorview().findviewbyid(android.r.id.content);     this.avatarimage = (imageview)rootview.findviewbyid(r.id.setavatar); }  @override protected void onpreexecute(){     progressdialog = new progressdialog(context);     progressdialog.setmessage("正在上传头像");     progressdialog.setcancelable(true);     progressdialog.show();     bytearrayoutputstream baos=new  bytearrayoutputstream();     if(avatar == null){         log.d("222","null");     }     //here got error,that avatar null;     avatar.compress(bitmap.compressformat.png,90,baos);     byte[] b=baos.tobytearray();     avatarstring= new string(b); }  @override protected boolean doinbackground(void... param){     //try upload image server }  @override protected void onpostexecute(boolean result){     ...//do work } 

}

it totally confused me,because got error when click on image in gallery,trying call crop(),i didn't finish crop,so bitmap data obvious null,it seems execute asynctask before finish crop.it's quite confusing,in crop() method start intent crop this:startactivityforresult(intent, 2);

but onactivityresult executed codes before result deliver back.

please help, in advance.this draw me crazy. :(

update: here more nullpointerobject stack trace code:

at com.example.allen.bubbles001.activity.uploadavatar.onpreexecute(userprofileactivity.java:229) @ android.os.asynctask.executeonexecutor(asynctask.java:587) @ android.os.asynctask.execute(asynctask.java:535) @ com.example.allen.bubbles001.activity.userprofileactivity.onactivityresult(userprofileactivity.java:168) @ android.app.activity.dispatchactivityresult(activity.java:5423) @ android.app.activitythread.deliverresults(activitythread.java:3347) @ android.app.activitythread.handlesendresult(activitythread.java:3394) 

the first entry point @

avatar.compress(bitmap.compressformat.png,90,baos); 

in uploadavatar.onpreexcute

the fourth entry point at

new uploadavatar(userprofileactivity.this, bitmap).execute(); 

in onactivityresult

it's bitmap null. seem pulling out of "data" intent, not idea in first place. intents have maximum size limit extras, , if bitmap large, go on limit. instead, recommend saving disk , putting filename in intent can read file instead.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -