我尝试滚动时,Android ListView抛出NullPointerException

时间:2011-07-26 01:45:39

标签: android listview android-listview

不完全确定这里发生了什么,所以我希望你们中的一个能指出我正确的方向。我在Tab框架中有一个ListView,由于某种原因,当我尝试滚动时它会加载但崩溃。

以下是我可以收集的所有信息

这是进入框架的Activity ...它将XML文件解析为优先级队列,获取附加的意图信息,并加载所需的页面。

public class QScoresFrameActivity extends ListActivity
{
private PriorityQueue<Score> mScores; 
private int mDifficulty;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scoresframe);
    mDifficulty = getIntent().getIntExtra(getResources().getString(R.string.difficulty), getResources().getInteger(R.integer.normal_level));  
    mScores = QGameGlobals.ParseScoresXML(this,mDifficulty);
    setListAdapter(new ScoresAdapter(this));
}

private class ScoresAdapter extends BaseAdapter 
{
    private LayoutInflater mInflater;

    public ScoresAdapter(Context context) 
    {
        mInflater = LayoutInflater.from(context);
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        Score thisScore = mScores.getByIndex(position);
        convertView = mInflater.inflate(R.layout.scoreview, null);

        TextView posT;
        TextView nameT;
        TextView scoreT;

        String name = thisScore.getName();
        int score = thisScore.getScore();

        posT  = (TextView)convertView.findViewById(R.id.position);
        nameT = (TextView)convertView.findViewById(R.id.scorerName);
        scoreT= (TextView)convertView.findViewById(R.id.scorerScore);

        posT.setText(String.valueOf(position + 1) + ". ");
        nameT.setText(name);
        scoreT.setText(String.valueOf(score));

        return convertView;
    }

    public int getCount() 
    {
        return mScores.getLength();
    }

    public Object getItem(int position) 
    {
        return this;
    }

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

}

包含列表的布局文件:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="vertical">
    <ListView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+android:id/list" />
</LinearLayout>

列表中每个项目的布局文件:

   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:padding="10dp" android:layout_width="fill_parent" android:baselineAligned="false" android:orientation="horizontal" android:layout_height="wrap_content">
        <TextView android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView" android:paddingRight="10dp" android:id="@+id/position" android:layout_width="50dp" android:textColor="@color/teal" ></TextView>
        <TextView android:id="@+id/scorerName" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="TextView" android:layout_width="wrap_content" android:layout_gravity="center_vertical"></TextView>
        <TextView android:id="@+id/scorerScore" android:text="TextView" android:textAppearance="?android:attr/textAppearanceLarge" android:paddingLeft="20dp" android:layout_height="wrap_content" android:gravity="right" android:textColor="@color/gold" android:layout_width="match_parent" android:layout_gravity="center_vertical"/>
</LinearLayout>

Priority Queue class ....

@SuppressWarnings("rawtypes")
public class PriorityQueue<T extends Comparable> extends Queue<T>
{
public PriorityQueue ()
{
    super();
}

@SuppressWarnings("unchecked")
@Override
public void enqueue(T item)
/* Enqueue a new object of type T into the queue based on its compareTo method.
 * Sorts from greatest to least.
 */
{
    TNode newNode = new TNode(item);

    if (root == null)
    {
        root = newNode;
        end = newNode;
    }
    else
    {
        TNode currNode = root;
        TNode lastNode = root; 

        while (true)
        {
            int compVal = item.compareTo(currNode.get());

            if (compVal == 1 || compVal == 0)
            {
                if (currNode == root)
                {
                    root = newNode;
                }
                else
                {
                    lastNode.next = newNode;
                    newNode.next = currNode;
                }
                newNode.next = currNode;
                break;
            }
            else
            {
                if (currNode == end)
                {
                    currNode.next = newNode; 
                    end = newNode;
                    break;
                }
                else
                {
                lastNode = currNode;
                currNode = currNode.next;
                }
            }
        }
    }
    length++;
}
}

感谢帮助。

4 个答案:

答案 0 :(得分:0)

这里的堆栈跟踪最有帮助。然而,随便,我猜测问题是你将充气机存储在适配器的构造函数中。相反,只需在getView()方法中调用getLayoutInflator()。如果没解决,请发布logcat。

答案 1 :(得分:0)

当我开始使用Android时,我会遇到这个问题... 我知道文档说明了您的列表android:id="@+android:id/list",但请尝试android:id="@id/android:list",如果碰巧您没有信息(空列表),请定义android:id="@id/android:empty"的视图占位符。

答案 2 :(得分:0)

问题在于自定义listadapter(ScoreAdapter)中的getView()函数。滚动时,listview会回收您的视图并调用getView()函数。将getView()函数更改为此以修复它:

public View getView(int position, View convertView, ViewGroup parent) 
{
    Score thisScore = mScores.getByIndex(position);
    if (convertView == null)
    {
        LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.scoreview, null);
    }
    TextView posT;
    TextView nameT;
    TextView scoreT;

    String name = thisScore.getName();
    int score = thisScore.getScore();

    posT  = (TextView)convertView.findViewById(R.id.position);
    nameT = (TextView)convertView.findViewById(R.id.scorerName);
    scoreT= (TextView)convertView.findViewById(R.id.scorerScore);

    posT.setText(String.valueOf(position + 1) + ". ");
    nameT.setText(name);
    scoreT.setText(String.valueOf(score));

    return convertView;
}

答案 3 :(得分:0)

啊,是的,你的问题肯定在PriorityQueue中。那个排队方法是各种各样的破碎,对不起。拿出铅笔和纸,然后浏览在队列中间添加的场景。我很确定我至少看到一个bug,可能多达三个。

- 好色