我试着理解为什么我的变量result
为空?我用Debug检查它,但我不明白为什么..我没有在我的设备中看到我的ImageView
这是我的代码:
public class ClassicView extends Activity {
private Content content;
public LinearLayout view;
ClassicView classicView;
JSONParser jParser;
Context context;
private Bitmap bitmap;
private ImageView downloadedImg;
private ProgressDialog simpleWaitDialog;
//RelativeLayout mainLayout;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( new ViewGroup.MarginLayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
public ClassicView() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
JSONParser jParser = null;
try {
jParser = new JSONParser("Json.txt");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
content = (Content)getIntent().getSerializableExtra("CONTENT");
//this.setClassicLabel(jParser.getContentViewWithId(content.getElemView()).getJSONArray("Label"));
//(jParser.getContentViewWithId(content.getElemView()).getJSONArray("Img"));
try {
this.setClassicImg();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setClassicLabel(JSONArray listLabel) throws JSONException {
RelativeLayout rl = new RelativeLayout(this);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
setContentView(rl);
for (int i = 0; i < listLabel.length(); i++) {
TextView tv = new TextView(this);
tv.setText(listLabel.getJSONObject(i).getString("text"));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (metrics.widthPixels * listLabel.getJSONObject(i).getDouble("size_x")), (int) (metrics.heightPixels * listLabel.getJSONObject(i).getDouble("size_y")));
params.leftMargin = (int) (metrics.widthPixels * listLabel.getJSONObject(i).getDouble("position_x"));
params.topMargin = (int) (metrics.heightPixels * listLabel.getJSONObject(i).getDouble("position_y"));
rl.addView(tv, params);
}
}
public void setClassicImg() throws JSONException, IOException, MalformedURLException {
RelativeLayout rl2 = new RelativeLayout(this);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
setContentView(rl2);
ImageView downloadedImg = new ImageView(this);
new DownloadImageTask(downloadedImg).execute("http://felicita-pizzeria.fr/images/logo.png");
}
/* To Load Image */
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView myimage;
public DownloadImageTask(ImageView bmImage) {
this.myimage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
myimage.setImageBitmap(result);
}
}`
有人可以帮我吗?谢谢
答案 0 :(得分:1)
它是null
,因为传递给任务构造函数的downloadedImg
为null。您的代码没有显示您如何找到ImageView的代码,但很可能您要么找到该布局中不存在的imageView,要么您忘记完全执行此操作。
答案 1 :(得分:1)
它提供nullpointer异常,因为您可能忘记初始化ImageView
new DownloadImageTask(downloadedImg).execute(..);
确保您已在班级中初始化了ImageView
。
ImageView downloadedImg =new ImageView(this);
使用以下代码从网址下载图片:
private Bitmap downloadBitmap(String url) {
// initilize the default HTTP client object
final DefaultHttpClient client = new DefaultHttpClient();
//forming a HttoGet request
final HttpGet getRequest = new HttpGet(url);
try {
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();
}
}
} catch (Exception e) {
Log.e("ImageDownloader", "Something went wrong while" +
" retrieving bitmap from " + url + e.toString());
}
return image;
}
更改您的代码如下:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView myimage;
public DownloadImageTask(ImageView bmImage) {
this.myimage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
mIcon11 = downloadBitmap(urldisplay);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
myimage.setImageBitmap(result);
}
}
<强>编辑:强>
这是我尝试过的代码,它的功能就像魅力一样。
public class MainActivity extends Activity {
Bitmap image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView mm=new ImageView(getApplicationContext());
new DownloadImageTask(mm).execute("http://felicita-pizzeria.fr/images/logo.png");
setContentView(mm);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private Bitmap downloadBitmap(String url) {
// initilize the default HTTP client object
final DefaultHttpClient client = new DefaultHttpClient();
//forming a HttoGet request
final HttpGet getRequest = new HttpGet(url);
try {
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();
}
}
} catch (Exception e) {
Log.e("ImageDownloader", "Something went wrong while" +
" retrieving bitmap from " + url + e.toString());
}
return image;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView myimage;
public DownloadImageTask(ImageView bmImage) {
this.myimage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
mIcon11 = downloadBitmap(urldisplay);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
myimage.setImageBitmap(result);
}
}
}
<强>输出:强>