作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个带有图片/文本的 GridView
,从不同的服务器加载(我只知道 URL)。我试图根据 the tutorial of ryac 修改我的代码.
在我的 Activity 文件中,我将 GAdapter
设置为我的 GridView
,如下所示:
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)
到目前为止,我从未在 Android 中使用过线程。如果有人能帮助我,我将不胜感激。
最佳答案
我在你做的最后输入了 getView()
views.put(position, v);
但它views
从未初始化。这就是您得到 java.lang.NullPointerException
为避免这种情况,请在您的 GAdapter 构造函数中初始化 HashMap 对象
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>();
}
关于android - 在 GridView 中加载异步图像 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8147958/
我正在开发一个需要能够平均三个数字的 Facebook 应用程序。但是,它总是返回 0 作为答案。这是我的代码: $y = 100; $n = 250; $m = 300; $number = ($y
我只是无法弄清楚这一点,也找不到任何对我来说有意义的类似问题。我的问题:我从数据库中提取记录,并在我的网页上以每个面板 12 条的倍数显示它们。因此,我需要知道有多少个面板可以使用 JavaScrip
我是一名优秀的程序员,十分优秀!