[英]Why i can't see ImageView on my device?
I tried to understand why my variable result
is null ? 我试着理解为什么我的变量
result
是null? I check it with Debug but I don't understand why.. I don't see my ImageView in my device And here is my code : 我用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);
}
}`
Somebody can help me please ? 有人可以帮帮我吗? thank you
谢谢
It's null
because downloadedImg
you pass to task constructor is null. 它是
null
因为您传递给任务构造函数的downloadedImg
为null。 Your code shows no code how you find that ImageView, but most likely you either look for imageView that does not exist in that layout or you forgot to do that completely. 您的代码没有显示您如何找到ImageView的代码,但很可能您要么找到该布局中不存在的imageView,要么您忘记完全执行此操作。
Its giving nullpointer exception because you might have forget to initialize the ImageView
which you are passing as parameter in your new DownloadImageTask(downloadedImg).execute(..);
它给空指针异常,因为你可能已经忘了初始化
ImageView
您所传递的参数在new DownloadImageTask(downloadedImg).execute(..);
Make sure you have initialized your ImageView
in your class. 确保已在班级中初始化了
ImageView
。
ImageView downloadedImg =new ImageView(this);
Use the below code to download the image from url: 使用以下代码从url下载图像:
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;
}
Change your code as below: 更改您的代码如下:
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);
}
}
EDITED: 编辑:
Here is my code which i have tried and which works like charm. 这是我尝试过的代码,它的功能就像魅力一样。
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);
}
}
} }
Output: 输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.