2011-11-16 16 views
5

में एसिंक्रोनस छवियों को लोड करना मेरे पास GridView चित्र/पाठ के साथ हैं, जो विभिन्न सर्वरों से लोड होते हैं (मुझे केवल यूआरएल पता है)। मैंने the tutorial of ryac के आधार पर अपना कोड संशोधित करने का प्रयास किया।ग्रिड व्यू (एंड्रॉइड)

मेरे गतिविधि फ़ाइल में, मैं इस तरह मेरी GridView को GAdapter सेट:

GridView mGridMain = (GridView)findViewById(R.id.gvMain); 
mGridMain.setAdapter(new GAdapter(this, listAppInfo)); 

मैं अपने खुद के एडाप्टर संशोधित और यह अनुकूल करने के लिए कोशिश की है:

public class GAdapter extends BaseAdapter { 

private Context mContext; 
private List<SiteStaff> mListAppInfo; 
private HashMap<Integer, ImageView> views; 

/** 
    * @param context 
    * @param list 
    */ 
public GAdapter(Context context, List<SiteStaff> list) { 
    mContext = context; 
    mListAppInfo = list; 
} 

@Override 
public int getCount() { 
    return mListAppInfo.size(); 
} 

@Override 
public Object getItem(int position) { 
    return mListAppInfo.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    SiteStaff entry = mListAppInfo.get(position); 

    if(convertView == null) { 
    LayoutInflater inflater = LayoutInflater.from(mContext); 
    convertView = inflater.inflate(R.layout.list_g, null); 
    } 

    ImageView v; 

    // get the ImageView for this position in the GridView.. 
    v = (ImageView)convertView.findViewById(R.id.ivIcon); 

    //set default image 
    v.setImageResource(android.R.drawable.ic_menu_gallery); 

    TextView tvName = (TextView)convertView.findViewById(R.id.tvName); 
    tvName.setText(entry.getName()); 


    Bundle b = new Bundle(); 
    b.putString("file", "http://www.test.com/mypict.jpg"); 
    b.putInt("pos", position); 

    // this executes a new thread, passing along the file 
    // to load and the position via the Bundle.. 
    new LoadImage().execute(b); 

    // puts this new ImageView and position in the HashMap. 
    views.put(position, v); 


    // return the view to the GridView.. 
    // at this point, the ImageView is only displaying the 
    // default icon.. 
    return v; 

} 




// this is the class that handles the loading of images from the cloud 
// inside another thread, separate from the main UI thread.. 
private class LoadImage extends AsyncTask<Bundle, Void, Bundle> { 

    @Override 
    protected Bundle doInBackground(Bundle... b) { 

    // get the file that was passed from the bundle.. 
    String file = b[0].getString("file"); 

    URL UrlImage; 
    Bitmap bm=null; 
    try { 
    UrlImage = new URL (file); 
    HttpURLConnection connection; 
    connection = (HttpURLConnection) UrlImage.openConnection(); 
    bm= BitmapFactory.decodeStream(connection.getInputStream()); 

    } catch (MalformedURLException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 


    // now that we have the bitmap (bm), we'll 
    // create another bundle to pass to 'onPostExecute'. 
    // this is the method that is called at the end of 
    // our task. like a callback function.. 
    // this time, we're not passing the filename to this 
    // method, but the actual bitmap, not forgetting to 
    // pass the same position along.. 

    Bundle bundle = new Bundle(); 
    bundle.putParcelable("bm", bm); 
    bundle.putInt("pos", b[0].getInt("pos")); 
    bundle.putString("file", file); // this is only used for testing.. 

    return bundle; 
    } 

    @Override 
    protected void onPostExecute(Bundle result) { 
    super.onPostExecute(result); 

    // just a test to make sure that the position and 
    // file name are matching before and after the 
    // image has loaded.. 
    Log.d("test", "*after: " + result.getInt("pos") + " | " + result.getString("file")); 

    // here's where the photo gets put into the 
    // appropriate ImageView. we're retrieving the 
    // ImageView from the HashMap according to 
    // the position.. 
    ImageView view = views.get(result.getInt("pos")); 

    // then we set the bitmap into that view. and that's it. 
    view.setImageBitmap((Bitmap) result.getParcelable("bm")); 
    } 

} 

} 

लेकिन नीचे त्रुटि है प्रदर्शित:

11-16 06:16:08.285: E/AndroidRuntime(517): FATAL EXCEPTION: main 
11-16 06:16:08.285: E/AndroidRuntime(517): java.lang.NullPointerException 
11-16 06:16:08.285: E/AndroidRuntime(517): at common.adapter.GAdapter.getView(GAdapter.java:139) 
11-16 06:16:08.285: E/AndroidRuntime(517): at android.widget.AbsListView.obtainView(AbsListView.java:1315) 
11-16 06:16:08.285: E/AndroidRuntime(517): at android.widget.GridView.onMeasure(GridView.java:932) 
11-16 06:16:08.285: E/AndroidRuntime(517): at android.view.View.measure(View.java:8171) 
11-16 06:16:08.285: E/AndroidRuntime(517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
11-16 06:16:08.285: E/AndroidRuntime(517): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012) 
11-16 06:16:08.285: E/AndroidRuntime(517): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381) 
11-16 06:16:08.285: E/AndroidRuntime(517): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 
11-16 06:16:08.285: E/AndroidRuntime(517): at android.view.View.measure(View.java:8171) 
11-16 06:16:08.285: E/AndroidRuntime(517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
11-16 06:16:08.285: E/AndroidRuntime(517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
11-16 06:16:08.285: E/AndroidRuntime(517): at android.view.View.measure(View.java:8171) 

अब तक, मैंने एंड्रॉइड में कभी भी थ्रेड का उपयोग नहीं किया। अगर कोई मेरी मदद कर सकता है तो मैं आभारी रहूंगा।

उत्तर

3

मैं अंत में getView() में झरना आप

views.put(position, v); 

कर रहे हैं लेकिन यह views कभी नहीं प्रारंभ। यही कारण है कि आप java.lang.NullPointerException

हो रही है अपने GAdapter में निर्माता में इस कृपया प्रारंभ हैश नक्शा वस्तु से बचने के लिए है

public GAdapter(Context context, List<SiteStaff> list) { 
    mContext = context; 
    mListAppInfo = list; 
    views = new HashMap<Integer, ImageView>(); 

} 

आप उदाहरण आप अपने प्रश्न में बात कर रहे हैं की जांच आप पाएंगे

public ImageAdapter (Context c, String f, ArrayList<HashMap<String, Object>> p) { 
     ... 
     views = new HashMap<Integer, ImageView>(); 
    } 
संबंधित मुद्दे