Android 使用异步任务延迟加载图像时出现Java空指针异常
我试图在适配器中使用AsyncTask延迟加载映像Android 使用异步任务延迟加载图像时出现Java空指针异常,android,android-asynctask,Android,Android Asynctask,我试图在适配器中使用AsyncTask延迟加载映像 public View getView(final int position,View convertView,ViewGroup parent){ Bitmap userAvatarStream = null,podIconStream = null ; Bitmap podIconStream1 = null; Bitmap podIconStream2 = null; View v =
public View getView(final int position,View convertView,ViewGroup parent){
Bitmap userAvatarStream = null,podIconStream = null ;
Bitmap podIconStream1 = null;
Bitmap podIconStream2 = null;
View v = convertView;
if (v == null){
LayoutInflater li = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.user_list_item,null);
}
user = users.get(position);
if(user != null){
URL userImageURL,podImageURL = null;
//TextView tk = (TextView)v.findViewById(R.id.text_key);
TextView firstname = (TextView)v.findViewById(R.id.follower_fullname);
ImageView user_avatar = (ImageView)v.findViewById(R.id.follower_user_avatar);
new LoadImage(user_avatar).execute();
TextView用户档案点击=(TextView)v.findViewById(R.id.follower_全名)
与评论者一致,您需要实际实例化用户_化身以使其可用。您打开流来拉虚拟形象,但实际上从未对其进行任何操作 编辑:
通过系统服务使用此选项。它更有效(有效我的意思是我从未有过这样的NPE)。与评论一致,你需要实际实例化用户的化身,使其可用。您打开流来拉虚拟形象,但实际上从未对其进行任何操作 编辑:
通过系统服务使用此选项。它更有效(我说的有效是指我从未有过这样的NPE)。user\u头像为空,不应该为空。你似乎没有把任何东西分配给user\u avatar。你在哪里实例化“user\u avatar”成员?发布代码,以便我们可以看到您在何处/何时执行此操作。可能会有帮助。已经发布了用户\u化身实例化部分。沿着这条线往下看:你确定在你的版面中有一个id为
follower\u user\u avatar
的ImageView
吗?是的。而且,如果我从代码中删除asynctask并且不使用任何threadinguser_avatar,那么代码就可以工作。如果avatar为null,那么它就不应该为null。你似乎没有把任何东西分配给user\u avatar。你在哪里实例化“user\u avatar”成员?发布代码,以便我们可以看到您在何处/何时执行此操作。可能会有帮助。已经发布了用户\u化身实例化部分。沿着这条线往下看:你确定在你的版面中有一个id为follower\u user\u avatar
的ImageView
吗?是的。而且,如果我从代码中删除asynctask而不使用任何threading@Hiccup你什么时候调用任务?在onCreate(Bundle)中?在onResume()中尝试。我认为这不重要,但它可能是。这是一个适配器,而不是一个活动。更好的是,您可以发布整个适配器。getView(…)代码吗?不要使用Context.getSystemService。相反,你喜欢我在上面发布的东西。做了改变。错误仍在继续。@Hiccup您何时调用任务?在onCreate(Bundle)中?在onResume()中尝试。我认为这不重要,但它可能是。这是一个适配器,而不是一个活动。更好的是,您可以发布整个适配器。getView(…)代码吗?不要使用Context.getSystemService。相反,你喜欢我在上面发布的东西。做了改变。错误仍在继续。
userProfileClick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String uID = user.getID();
//Pass User id to another Intent(UserProfile)
Intent userIntent = new Intent(activity,UserProfileMainActivity.class);
userIntent.putExtra("userID",uID);
activity.startActivity(userIntent);
}
});
ListView podlist = (ListView)v.findViewById(R.id.user_list);
ArrayList<Cr> List = new ArrayList<Cr>();
for(Crb c : user.crList){
List.add(c);
}
//new LoadImage(user_avatar).execute();
UserFollowingPodListAdapter podadapter = new UserFollowingPodListAdapter(activity, R.layout.user_crumbs_pod_list_item,crumbsUpList,activity);
podlist.setAdapter(podadapter);
}
return v;
}
class LoadImage extends AsyncTask<String, Void, String>{
private ImageView imv;
private String path;
Bitmap userAvatarStream = null ;
final Bitmap podIconStream = null;
ProgressDialog dialog;
@Override
protected void onPreExecute(){
//Setting all the variables by getting the ids from the layout
return;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
URL userImageURL,podImageURL = null;
try {
userImageURL = new URL(user.imageUrl);
if (user == null)
return null;
userAvatarStream = BitmapFactory.decodeStream(userImageURL.openConnection().getInputStream());
if (userAvatarStream == null)
return null;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result){
user_avatar.setImageBitmap(userAvatarStream);
return;
}
}
TextView firstname = (TextView)v.findViewById(R.id.follower_fullname);
ImageView user_avatar = (ImageView)v.findViewById(R.id.follower_user_avatar);
View v = LayoutInflater.from(context).inflate(R.layout.mybigdamnlayout, null);