2016-09-04 6 views
-1

Ich versuche, ein Bild aus dem Internet auf die ImageView anzuzeigen, aber immer NullPointerException.NullPointerException beim Herunterladen des Bildes

Fehlerprotokoll:

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

Code:

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); 
     } 
    } 
} 
+0

Ihre 'bmImage' ist null, vielleicht sollten Sie die ID überprüfen. –

+0

Link in Ausführen() haben keine Erweiterung. –

+0

ID ist in Ordnung und ich verpasste die Erweiterung beim Einfügen des Codes .. immer noch nullpointerexception –

Antwort

1

(ImageView) findViewById(R.id.imageViewID) kehrt null

Sie benötigen einen benutzerdefinierten Adapter, der wie folgt aber bitte betrachten Sie dieses Beispiel nur als aussehen sollte So zeigen Sie Ihnen die Art von Dingen, die Sie tun müssen: Es gibt bessere Möglichkeiten, das ViewHolder-Muster zu verwenden.

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

Haben Sie von Picasso und Glide gehört? Bitte Google es ...

Verwandte Themen