[英]Why i can't see ImageView on my device?
我試着理解為什么我的變量result
是null? 我用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);
}
}`
有人可以幫幫我嗎? 謝謝
它是null
因為您傳遞給任務構造函數的downloadedImg
為null。 您的代碼沒有顯示您如何找到ImageView的代碼,但很可能您要么找到該布局中不存在的imageView,要么您忘記完全執行此操作。
它給空指針異常,因為你可能已經忘了初始化ImageView
您所傳遞的參數在new DownloadImageTask(downloadedImg).execute(..);
確保已在班級中初始化了ImageView
。
ImageView downloadedImg =new ImageView(this);
使用以下代碼從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;
}
更改您的代碼如下:
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);
}
}
}
輸出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.