使用Async Task进行延迟加载图像时出现Java空指针异常

时间:2012-02-07 17:08:25

标签: 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 = 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 userProfileClick =(TextView)v.findViewById(R.id.follower_fullname);

        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;

     }

}

}

我在onPostExecute上遇到了Java Null Poitner异常。

我调试了代码,userAvatarStream不为null。尝试了if / else,尝试/ catch尝试找出null异常发生的位置,并找出其中:user_avatar.setImageBitmap(userAvatarStream);但是,不知道它为什么会发生或如何删除它。任何帮助都感激不尽。

编辑:

TextView firstname = (TextView)v.findViewById(R.id.follower_fullname);
            ImageView user_avatar = (ImageView)v.findViewById(R.id.follower_user_avatar);

1 个答案:

答案 0 :(得分:1)

与评论员一致,您需要实际实例化user_avatar才能使用它。你打开流来拉动头像,但实际上从未对它做任何事情。

编辑:

View v = LayoutInflater.from(context).inflate(R.layout.mybigdamnlayout, null);

通过SystemService使用它。它更有效(通过有效我的意思是我从来没有这样的NPE)。