下载图像时出现NullPointerException

时间:2016-09-04 10:06:45

标签: java android nullpointerexception

我正在尝试将图像从互联网显示到ImageView但是获得NullPointerException

错误日志:

java.lang.NullPointerException at com.example.user.labone.MainActivity$DownloadImageTask.onPostExecute(MainActivity.java:176)

代码:

public class MainActivity extends ActionBarActivity {

    String jsonString;

    private static final String TAG_RESULTS="result";
    private static final String TAG_ID = "id";
    private static final String TAG_TITLE = "title";
    private static final String TAG_IMG= "src";
    private static final String TAG_DESC ="desc";

    JSONArray dataArray = null;

    ArrayList<HashMap<String, String>> dataArrayList;

    ListView list;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list = (ListView) findViewById(R.id.listView);
        //imageView=(ImageView) findViewById(R.id.imageView);

        dataArrayList = new ArrayList<HashMap<String,String>>();
        getData();
    }


    protected void showList(){
        try {
            JSONObject jsonObj = new JSONObject(jsonString);
            dataArray = jsonObj.getJSONArray(TAG_RESULTS);

            for(int i = 0; i< dataArray.length(); i++){
                JSONObject c = dataArray.getJSONObject(i);
                String idString = c.getString(TAG_ID);
                String titleString = c.getString(TAG_TITLE);
                String imgString = c.getString(TAG_IMG);
                String descString = c.getString(TAG_DESC);

                HashMap<String,String> dataHashMap = new HashMap<String,String>();

                dataHashMap.put(TAG_ID,idString);
                dataHashMap.put(TAG_TITLE,titleString);
                dataHashMap.put(TAG_IMG,imgString);
                dataHashMap.put(TAG_DESC,descString);

                // show The Image
                new DownloadImageTask((ImageView) findViewById(R.id.imageViewID)).execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
                Log.i("TAG", imgString);

                dataArrayList.add(dataHashMap);
            }

            ListAdapter adapter = new SimpleAdapter(
                    MainActivity.this, dataArrayList, R.layout.list_item,
                    new String[]{TAG_ID, TAG_TITLE, TAG_DESC},
                    new int[]{R.id.id, R.id.title, R.id.desc}
            );

            list.setAdapter(adapter);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public void getData(){
        class GetDataJSON extends AsyncTask<String, Void, String>{

            @Override
            protected String doInBackground(String... params) {
                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                HttpPost httppost = new HttpPost("http://geekysoft.com.pk/labone/labone_conn.php");

                // Depends on your web service
                httppost.setHeader("Content-type", "application/json");

                InputStream inputStream = null;
                String result = null;
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();

                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (Exception e) {
                    // Oops
                }
                finally {
                    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result){
                jsonString =result;
                showList();
            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute();
    }

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        @Override
        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();
            }
            Log.i("BITMAP", "BITMAP SIZE IS : " + mIcon11.getByteCount());
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

(ImageView) findViewById(R.id.imageViewID)返回null

您需要一个自定义适配器,它应该如下所示,但请将此示例视为向您展示您需要执行的操作的方式:有更好的方法可以像使用ViewHolder模式一样。

public class ListWithImageAdapter extends ArrayAdapter<String>{

    private final Context mContext;
    private final String[] mTexts;

    public ListWithImageAdapter(Context context, String[] texts) {
        super(context, R.layout.list_item_payment_type, texts);
        mContext = context;
        mTexts = texts;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        View rowView= LayoutInflater.from(mContext).inflate(R.layout.list_item, null, true);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.img); // here you get the image

        return rowView;
    }
}

你听说过毕加索和格莱德吗?请谷歌...