我在尝试运行我的应用程序时正在开发一个Android应用程序,我得到了以下异常。
08-12 16:57:41.546 25559-25603/com.ringee.app E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #5
Process: com.ringee.app, PID: 25559
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.ringee.app.ImageUploadActivity$4$1.doInBackground(ImageUploadActivity.java:169)
at com.ringee.app.ImageUploadActivity$4$1.doInBackground(ImageUploadActivity.java:158)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
08-12 16:57:41.982 25559-25559/com.ringee.app E/WindowManager﹕ android.view.WindowLeaked: Activity com.ringee.app.ImageUploadActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41bfa110 V.E..... R......D 0,0-456,144} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:346)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:286)
at com.ringee.app.ImageUploadActivity$4.onClick(ImageUploadActivity.java:156)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
this is my programming code
package com.ringee.app;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ringee.app.dataaccess.DatabaseHelper;
import com.ringee.app.dataobjects.MediaMO;
import com.ringee.app.dataobjects.UserMO;
import com.ringee.app.delegates.MediaDelegates;
import com.ringee.app.delegates.UserDelegate;
import com.ringee.app.utility.Constants;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class ImageUploadActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
//EditText initialized here
EditText Myusername;
EditText user;
private MediaDelegates mediaDelegates = new MediaDelegates();
private File imgFile;
private DatabaseHelper dbHelper;
private Context context;
private UserMO userMO = new UserMO();
private ImageView imageView;
private EditText eText;
private Gson gson = new Gson();
private ProgressDialog prgDialog;
private MediaMO mediaMO = null;
private UserDelegate userDelegates = new UserDelegate();
private String text;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.image_upload, menu);
return true;
}
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageupload);
context = getApplicationContext();
dbHelper = new DatabaseHelper(context);
userMO = dbHelper.getRingeeUserData(1);
imageView = (ImageView) findViewById(R.id.imgView);
prgDialog = new ProgressDialog(this);
// Set Progress Dialog Text
prgDialog.setMessage("Please wait...");
// Set Cancelable as False
prgDialog.setCancelable(false);
prgDialog.show();
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... arg0) {
String mediaMo = mediaDelegates.getFileFromServer(userMO, context);
if (!mediaMo.equals("null")) {
mediaMO = gson.fromJson(mediaMo, new TypeToken<MediaMO>() {
}.getType());
try {
URL url = new URL(mediaMO.getFileSrcLink());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (IOException e) {
Log.i(Constants.TAG, e.toString());
}
} else
mediaMO = null;
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_user);
return largeIcon;
}
@Override
protected void onPostExecute(Bitmap myBitmap) {
prgDialog.cancel();
imageView.setImageBitmap(myBitmap);
}
}.execute(null, null, null);
//EditText added here for get current username
Myusername = (EditText) findViewById(R.id.userEditText);
//String s = userMO.getUserName();
String s=userMO.getUserName();
Myusername.setText(s);
Myusername.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final String str1 = Myusername.getText().toString();
Intent int1 = new Intent(ImageUploadActivity.this, NameUpdateActivity.class);
Bundle bundle = new Bundle();
bundle.putString("userdata1", str1);
int1.putExtras(bundle);
//(int1);
startActivityForResult(int1, 0);
}
});
Button buttonAddImage = (Button) findViewById(R.id.buttonAddPicture);
buttonAddImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
MediaMO mediaMO1 = new MediaMO();
mediaMO1.setRingeeUserId(userMO.getRingeeUserId());
}
});
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
prgDialog.show();
if (mediaMO == null) {
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... arg0) {
MediaMO mediaMO1 = new MediaMO();
mediaMO1.setRingeeUserId(userMO.getRingeeUserId());
mediaMO1.setIsType(Constants.IS_TYPE_PROFILE_IMAGE);
String mediaMo = mediaDelegates.uploadFileToServer(mediaMO1, context, imgFile);
if (!mediaMo.equals("null")) {
mediaMO = gson.fromJson(mediaMo, new TypeToken<MediaMO>() {
}.getType());
try {
URL url = new URL(mediaMO.getFileSrcLink());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (IOException e) {
Log.i(Constants.TAG, e.toString());
}
}
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_user);
return largeIcon;
}
@Override
protected void onPostExecute(Bitmap myBitmap) {
prgDialog.cancel();
imageView.setImageBitmap(myBitmap);
}
}.execute(null, null, null);
} else {
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... arg0) {
String mediaMo = mediaDelegates.updateFileToServer(mediaMO, context, imgFile);
if (!mediaMo.equals("null")) {
mediaMO = gson.fromJson(mediaMo, new TypeToken<MediaMO>() {
}.getType());
try {
URL url = new URL(mediaMO.getFileSrcLink());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (IOException e) {
Log.i(Constants.TAG, e.toString());
}
}
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_user);
return largeIcon;
}
@Override
protected void onPostExecute(Bitmap myBitmap) {
prgDialog.cancel();
imageView.setImageBitmap(myBitmap);
}
}.execute(null, null, null);
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
imgFile = new File(picturePath);
}
}
当运行时我点击加载图片buttoni得到了这个异常。按钮按钮我用来加载图片任何人都可以帮我解决这个异常。
答案 0 :(得分:1)
你应该在你的输入流中传递图像的url。
InputStream is = (InputStream) new URL(urlofimage).getContent();
返回BitmapFactory.decodeStream(是);