Android 从URL获取图像时出现空指针异常
我从JSON数组中给出的URL获取图像。我已经解析了整个数据,并成功地将数据带到下一个活动,但当我尝试使用位图解码URL时,它在onPostExecute方法处显示空指针异常 传入OnPostExecute方法返回的结果参数存储值“android.graphics”。Bitmap@418aad30“只有当我没有添加将位图设置为图像视图的语句时,我才能得到这个结果,即“img.setImageBitmap(newBitmap);” 我的Java文件是::Android 从URL获取图像时出现空指针异常,android,bitmap,nullpointerexception,Android,Bitmap,Nullpointerexception,我从JSON数组中给出的URL获取图像。我已经解析了整个数据,并成功地将数据带到下一个活动,但当我尝试使用位图解码URL时,它在onPostExecute方法处显示空指针异常 传入OnPostExecute方法返回的结果参数存储值“android.graphics”。Bitmap@418aad30“只有当我没有添加将位图设置为图像视图的语句时,我才能得到这个结果,即“img.setImageBitmap(newBitmap);” 我的Java文件是:: package com.ourcast.p
package com.ourcast.pocketweather;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
public class DisplayWeathernew extends Activity {
String city, date, maximumTemp, minimumTemp, description, weatherImageUrl;
ImageView img;
ListView weatherList;
List <Bean> bean;
Bitmap myBitmap, newBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_weather);
img = (ImageView) findViewById(R.id.imgweather);
weatherList = (ListView) findViewById(R.id.lvWeather);
for(int i=0; i<WeatherHome.arrayList.size(); i++)
{
city = WeatherHome.arrayList.get(i).getCity(); //WeatherHome.arrayList.get(index).getCity();
date = WeatherHome.arrayList.get(i).getDate();
maximumTemp = WeatherHome.arrayList.get(i).getMaximumTemprature();
minimumTemp = WeatherHome.arrayList.get(i).getMinimumTemprature();
description = WeatherHome.arrayList.get(i).getDescription();
weatherImageUrl = WeatherHome.arrayList.get(i).getImageUrl();
Toast.makeText(this, "City "+city+" Date "+date+" Minimum "+minimumTemp+" Maximum "+maximumTemp+" Desc "+description+" URL "+weatherImageUrl, Toast.LENGTH_LONG).show();
}
new ImageDownload().execute();
/*Log.i("TAG", "FIIIIIIIIIIINNNNNNNNNNNNNNNNNEEEEEEEEEEEEEE");
//bean.add(new Bean("Hello", "Hei", "Heya", "Hola", "Worked!!"));
Log.i("TAG", "FIII");
CustomBaseAdapter baseAdapter = new CustomBaseAdapter(this, bean);
weatherList.setAdapter(baseAdapter);*/
}
private class ImageDownload extends AsyncTask<String, Void, Bitmap>{
protected Bitmap doInBackground(String... arg0){
try{
Log.e("src",weatherImageUrl);
URL url = new URL(weatherImageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap","returned");
return myBitmap;
}
catch(Exception e){
e.printStackTrace();
return null;
}
}
protected void onPostExecute(Bitmap result){
Toast.makeText(DisplayWeathernew.this, "Result"+result, Toast.LENGTH_LONG).show();
if(result!=null){
img.setImageBitmap(result);
}else
{
img.setImageResource(R.drawable.button_display_weather);
}
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
WeatherHome.arrayList.clear();
super.onBackPressed();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,0,0,"Exit");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==0)
{
finish();
}
return super.onOptionsItemSelected(item);
}
}
03-22 01:16:01.670: E/src(3047): http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png
03-22 01:16:02.160: E/Bitmap(3047): returned
03-22 01:16:02.170: W/dalvikvm(3047): threadid=1: thread exiting with uncaught exception (group=0x4104a450)
03-22 01:16:02.190: E/AndroidRuntime(3047): FATAL EXCEPTION: main
03-22 01:16:02.190: E/AndroidRuntime(3047): java.lang.NullPointerException
03-22 01:16:02.190: E/AndroidRuntime(3047): at com.ourcast.pocketweather.DisplayWeathernew$ImageDownload.onPostExecute(DisplayWeathernew.java:90)
03-22 01:16:02.190: E/AndroidRuntime(3047): at com.ourcast.pocketweather.DisplayWeathernew$ImageDownload.onPostExecute(DisplayWeathernew.java:1)
03-22 01:16:02.190: E/AndroidRuntime(3047): at android.os.AsyncTask.finish(AsyncTask.java:631)
03-22 01:16:02.190: E/AndroidRuntime(3047): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-22 01:16:02.190: E/AndroidRuntime(3047): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-22 01:16:02.190: E/AndroidRuntime(3047): at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 01:16:02.190: E/AndroidRuntime(3047): at android.os.Looper.loop(Looper.java:137)
03-22 01:16:02.190: E/AndroidRuntime(3047): at android.app.ActivityThread.main(ActivityThread.java:4802)
03-22 01:16:02.190: E/AndroidRuntime(3047): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 01:16:02.190: E/AndroidRuntime(3047): at java.lang.reflect.Method.invoke(Method.java:511)
03-22 01:16:02.190: E/AndroidRuntime(3047): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:813)
03-22 01:16:02.190: E/AndroidRuntime(3047): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:580)
03-22 01:16:02.190: E/AndroidRuntime(3047): at dalvik.system.NativeStart.main(Native Method)
03-22 01:16:08.090: E/Trace(3124): error opening trace file: No such file or directory (2)
给我一些可以帮助我的建议。看起来
img
为空。检查以确保imgweather
处于R.layout.activity\u display\u weather
中。您对lvWeather
使用了不同的约定,因此可能您的意思是imgWeather
?那么,img==null再次检查imgWeather
是否是活动显示\u weather.xml
中图像视图的id
。如果是,请尝试清理项目。ImageView是java对象。Java对象可以为null查看的文档,如果找到,则返回视图,否则返回null。@codeMagic是的,它是imgWeather而不是imgWeather。它可以显示图像,但我只得到显示中的最后一个图像,而不是我想显示我在循环中得到的所有图像。