NullPointerException在onActivityResult中使用AsyncTask时出错
我想从图库中选择图像并裁切,然后将图像上传到服务器。如果上传成功,更改视图,如果上传失败,则会发出消息;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
回答:
这可能是位图为null。你似乎把它从“数据”意图中拉出来,这首先可能不是一个好主意。 Intents对extras有一个最大的大小限制,如果Bitmap很大,你很容易超过限制。相反,我建议将它保存到磁盘并将文件名作为额外文件放入Intent中,以便您可以从文件中读取它。
以上是 NullPointerException在onActivityResult中使用AsyncTask时出错 的全部内容, 来源链接: utcz.com/qa/260163.html