从URL获取图像时出现空指针异常

时间:2014-03-21 19:54:24

标签: android bitmap nullpointerexception

我从JSON Array中给出的URL获取图像。我已经解析了整个数据并成功地将数据带到了下一个活动,但是当我尝试使用Bitmap解码URL时,它在onPostExecute方法中显示Null Pointer Exception。

在OnPostExecute方法中传递的Result参数返回存储值" android.graphics.Bitmap@418aad30"只有当我没有添加语句来将Bitmap设置为Image View即#34; img.setImageBitmap(newBitmap);"时,我才会这样做。

我的Java文件是:::

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);

    }
}

一旦我添加语句,它就会抛出Null Pointer Exception,甚至不显示Result的toast并关闭应用程序。

当我在&#34; myBitmap = BitmapFactory.decodeStream(输入)之后添加TOAST时,也在doInBackground方法中;&#34;那么下面的代码既没有执行,也没有执行TOAST打印,结果显示空值。

我的日志是:::

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)

建议一些有用的东西。

1 个答案:

答案 0 :(得分:1)

看起来img为空。检查以确保imgweather位于R.layout.activity_display_weather。您对lvWeather使用不同的约定,所以也许您的意思是imgWeather