我想從圖庫中選擇圖像並裁切,然後將圖像上傳到服務器。如果上傳成功,更改視圖,如果上傳失敗,則會發出消息;NullPointerException在onActivityResult中使用AsyncTask時出錯
我使用onActivityResult來管理這個,我用的AsyncTask使HTTP POST,駁回progressDialog,並保存圖像,然後,我跑到這個錯誤
java.lang.RuntimeException: Failure delivering result ResultInfo...
,並通過
Caused by: java.lang.NullPointerException
引起
在我的AsyncTask代碼;
這裏是我的按鈕裁切
avatarButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent,1);
}
});
和onActivityResult代碼
@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);
}
裁剪方法
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);
}
和的AsyncTask類
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 i Got error,that avatar is 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 to server
}
@Override
protected void onPostExecute(Boolean result){
...//do some work
}
}
它完全困惑我,因爲我得到這個錯誤,當我點擊圖片庫中的圖片,試圖調用Crop(),我沒有完成我的作物,所以位圖數據是明顯的空,它似乎執行AsyncTask之前我完成我的作物。這是相當混亂,在crop()方法我開始意圖這樣裁剪:startActivityForResult(intent, 2);
但onActivityResult執行代碼之前結果被傳回。
請幫助,在此先感謝。這真讓我着迷。 :(
更新: 下面是一些更NullPointerObject堆棧跟蹤代碼:
at com.example.allen.bubbles001.Activity.UploadAvatar.onPreExecute(UserProfileActivity.java:229)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
at android.os.AsyncTask.execute(AsyncTask.java:535)
at com.example.allen.bubbles001.Activity.UserProfileActivity.onActivityResult(UserProfileActivity.java:168)
at android.app.Activity.dispatchActivityResult(Activity.java:5423)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
的第一項點
avatar.compress(Bitmap.CompressFormat.PNG,90,baos);
在UploadAvatar.onPreExcute
的第四個條目點
new UploadAvatar(UserProfileActivity.this, bitmap).execute();
in onActivityResult
你能分享更多的導致NullPointerException的棧跟蹤嗎?如果您可以指出異常處於什麼位置,它會很有幫助。 –
@MichaelKrause更新了一些堆棧跟蹤代碼 –