Android的ListView控件抛出NullPointerException异常,当我尝试滚动 [英] Android ListView throwing NullPointerException when I try to scroll

查看:200
本文介绍了Android的ListView控件抛出NullPointerException异常,当我尝试滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不完全知道是怎么回事就在这里,所以我希望你们可以点我在正确的方向。我有一个标签帧中的ListView和出于某种原因,它加载而崩溃,当我尝试滚动。

Not exactly sure what is going on here so I am hoping one of you can point me in the right direction. I have a ListView within a Tab frame and for some reason, it loads but crashes when I try to scroll.

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

Here is all of the information I could gather

下面是进入框架中的活动......它解析XML文件转换为优先级队列,获取附加的意图的信息,并加载所需的页面。

Here is the Activity that goes into the frame... it parses an XML file into a priority queue, gets the attached intent information, and loads the desired page.

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;
    }
}

}

保存列表中的布局文件:

The layout file that holds the list:

<?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>

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

The layout file for each item in the list:

   <?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++;
}
}

鸭preciate的帮助。

Appreciate the help.

推荐答案

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

A stack trace here would be most helpful. However, offhand, I would guess that the problem is you are storing the inflator in the adapter's constructor. Instead, just call getLayoutInflator() inside your getView() method. If that doesn't resolve it, please do post the logcat.

这篇关于Android的ListView控件抛出NullPointerException异常,当我尝试滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆