English 中文(简体)
A. 与ArrayList Falpointer一同居住的清单
原标题:Listview populate with ArrayList nullpointer
  • 时间:2012-01-15 04:36:35
  •  标签:
  • java
  • android

I m trying to make a listview from an arraylist using the tutorial i found here. The rows can either be a team name or a player name. Right now it works fine when I scroll down, but gives a nullpointer when I scroll back up, usually because a row that is associated with a name is trying to set the team name textview or vice versa (i determined this from my log statements). Ex. a view with isPlayerName set to true gives a nullpointer on the line where I set viewholder.player_name. I think it has something to do with the getTag() method not referencing the right object. The tutorial worked fine when I used only player rows, (probably because all of viewholders objects were instaiated). Here is my listadapter: private class CustomAdapter extends BaseAdapter {

    private ArrayList<ArrayList<String>> player_array;
    private LayoutInflater inflater;

    public CustomAdapter(Context context, ArrayList<ArrayList<String>> array){
        player_array = array;
        inflater = LayoutInflater.from(context);
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewholder= new ViewHolder();
        boolean isPlayerName = (player_array.get(position).size() > 1 ? true : false);
        if (convertView == null){
        if (isPlayerName){
            convertView = inflater.inflate(R.layout.player_list_item, null);
            viewholder.player_name = (TextView)convertView.findViewById(R.id.player_name);
            viewholder.layout = (RelativeLayout)convertView.findViewById(R.id.player_list_item_layout);
        }
        else {
            convertView = inflater.inflate(R.layout.team_item, null);
            viewholder.team_icon = (ImageView)convertView.findViewById(R.id.team_logo);
            viewholder.team_name = (TextView)convertView.findViewById(R.id.team_name);
        }
        convertView.setTag(viewholder);
        }
        else {
            viewholder = (ViewHolder)convertView.getTag();
        }
        if (isPlayerName){
            try {
                viewholder.player_name.setText((player_array.get(position)).get(0));
                viewholder.layout.setOnClickListener(new CustomOnClickListener(player_array.get(position).get(1)));
            }
            catch (NullPointerException e){
                Log.d("NULL", "is viewholder null? " + (viewholder == null));
                Log.d("NULL", "is player name? " + isPlayerName);
                Log.d("NULL", "is getTag null? " + (convertView.getTag() == null));
                Log.d("NULL", "is team name null " + (viewholder.team_name == null));
                Log.d("NULL", "is team icon null " + (viewholder.team_icon == null));
                Log.d("NULL", "is player name null " + (viewholder.player_name == null));
                Log.d("NULL", "is link null? " + (player_array.get(position).get(1) == null));
                Log.d("NULL", "is layout null? " + (viewholder.layout == null));
            }

        }
        else {
            String team_name = player_array.get(position).get(0);
            try {
            viewholder.team_name.setText(Character.toUpperCase(team_name.charAt(0)) + team_name.substring(1));
            }
            catch (NullPointerException e){
                Log.d("NULL", "is player name? " + isPlayerName);
                Log.d("NULL", "is getTag null? " + (convertView.getTag() == null));
                Log.d("NULL", "is team name null " + (viewholder.team_name == null));
                Log.d("NULL", "is team icon null " + (viewholder.team_icon == null));
                Log.d("NULL", "is player name null " + (viewholder.player_name == null));
            }
            if (team_name.equals("hawks")){
                viewholder.team_icon.setBackgroundResource(R.drawable.hawks);
            }
            else if (team_name.equals("lions")){
                viewholder.team_icon.setBackgroundResource(R.drawable.lions);
            }
            else if (team_name.equals("sparks")){
                viewholder.team_icon.setBackgroundResource(R.drawable.sparks);
            }
            else if (team_name.equals("bulls")){
                viewholder.team_icon.setBackgroundResource(R.drawable.bulls);
            }
            else if (team_name.equals("renegades")){
                viewholder.team_icon.setBackgroundResource(R.drawable.renegades);
            }
            else if (team_name.equals("poppiezz")){
                viewholder.team_icon.setBackgroundResource(R.drawable.poppiezz);
            }
            else if (team_name.equals("mambas")){
                viewholder.team_icon.setBackgroundResource(R.drawable.mambas);
            }
            else if (team_name.equals("78sixers")){
                viewholder.team_icon.setBackgroundResource(R.drawable.sixers);
            }
            else if (team_name.equals("brooklynites")){
                viewholder.team_icon.setBackgroundResource(R.drawable.brooklynites);
            }
            else if (team_name.equals("blazers")){
                viewholder.team_icon.setBackgroundResource(R.drawable.blazers);
            }
            else if (team_name.equals("warriors")){
                viewholder.team_icon.setBackgroundResource(R.drawable.warriors);
            }
            else {
                viewholder.team_icon.setBackgroundResource(R.drawable.basketball);
            }
        }
        return convertView;
    }

}

And here is my viewHolder, I use the same one for both types of rows, but only certain fields get instantiated depending on if it is a team or a player row

    private class ViewHolder {

    ImageView team_icon;
    TextView player_name;
    RelativeLayout layout;
    TextView team_name;
}

之所以如此,是因为我不理解“骗局”方法?

最佳回答

凯文

根据你的代码,你似乎正确理解getTag()tag()。 就如实,请让我对你说一下:

“标签”是另一个目标(任何目标)。 它没有类型的特殊配对,但往往被用于为非数据目标(如你的视像)提供数据目标。 不幸的是,这意味着,如果使用“标签”,你必须自行检查。

The most common issue with "tags" is that many developers grab the "tag" of the wrong View and, hence, get the wrong or even null data. This does not seem to be the case with your code. However, you have all of the Log.d() statements. We would certainly be able to help you more easily if we had a copy of that logcat. wink wink nudge nudge

Hope this helps,

Fuzzical Carloic

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签