我在其他几篇文章中读到,当我收到其中一个错误时,运行时包与编译包不同。看了logcat很长一段时间后,我似乎无法弄清楚哪个包以及在哪里更改它。
05-30 14:57:50.950: E/AndroidRuntime(823): FATAL EXCEPTION: main
05-30 14:57:50.950: E/AndroidRuntime(823): Process: com.example.bitmapdisplay, PID: 823
05-30 14:57:50.950: E/AndroidRuntime(823): java.lang.VerifyError: com/example/bitmapdisplay/MainActivity$displayImages
05-30 14:57:50.950: E/AndroidRuntime(823): at com.example.bitmapdisplay.MainActivity.onCreate(MainActivity.java:38)
05-30 14:57:50.950: E/AndroidRuntime(823): at android.app.Activity.performCreate(Activity.java:5231)
05-30 14:57:50.950: E/AndroidRuntime(823): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-30 14:57:50.950: E/AndroidRuntime(823): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-30 14:57:50.950: E/AndroidRuntime(823): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-30 14:57:50.950: E/AndroidRuntime(823): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-30 14:57:50.950: E/AndroidRuntime(823): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-30 14:57:50.950: E/AndroidRuntime(823): at android.os.Handler.dispatchMessage(Handler.java:102)
05-30 14:57:50.950: E/AndroidRuntime(823): at android.os.Looper.loop(Looper.java:136)
05-30 14:57:50.950: E/AndroidRuntime(823): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-30 14:57:50.950: E/AndroidRuntime(823): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 14:57:50.950: E/AndroidRuntime(823): at java.lang.reflect.Method.invoke(Method.java:515)
05-30 14:57:50.950: E/AndroidRuntime(823): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-30 14:57:50.950: E/AndroidRuntime(823): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-30 14:57:50.950: E/AndroidRuntime(823): at dalvik.system.NativeStart.main(Native Method)
这是我的Java代码。我有一种感觉,它在AsyncTask类中弄乱了:
package com.example.bitmapdisplay;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import com.fasterxml.jackson.core.JsonParseException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
public class MainActivity extends Activity {
Bitmap image;
BitmapDrawable bd;
ImageView temp;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
temp = (ImageView) findViewById(R.id.ivImage);
displayImages display = new displayImages();
display.execute();
}
public class displayImages extends AsyncTask <Void, Void, Bitmap> {
@Override
protected Bitmap doInBackground(Void... params) {
try {
image = downloadBitmap("http://i1.cpcache.com/product_zoom/617914535/dickbutt_2_mug.jpg?side=Back&height=250&width=250&padToSquare=true");
} catch (JsonParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
protected Void onPostExecute(Void...voids ) {
temp.setImageBitmap(image);
return null;
}
}
private Bitmap downloadBitmap(String url) throws JsonParseException, IOException{
// initilize the default HTTP client object
final DefaultHttpClient client = new DefaultHttpClient();
//forming a HttoGet request
final HttpGet getRequest = new HttpGet(url);
HttpResponse response = client.execute(getRequest);
//check 200 OK for success
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode +
" while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
// getting contents from the stream
inputStream = entity.getContent();
// decoding stream data back into image Bitmap that android understands
image = BitmapFactory.decodeStream(inputStream);
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
return image;
}
}
答案 0 :(得分:0)
更改您的onPostExecute
,
// the param type here needs to be a bitmap,
// and use void (not Void) as the return.
// and add the missing @Override
// also, no need for the return null here as well
@Override
protected void onPostExecute(Bitmap image) {
temp.setImageBitmap(image);
}
可在此处找到一个很好的示例和文档: http://developer.android.com/reference/android/os/AsyncTask.html