custonlistadapter的空指针异常?

时间:2013-05-02 19:26:31

标签: android android-listview android-arrayadapter

public class ListPage extends ListActivity
{
    TheLists a[] = new TheLists[1];
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.list_page);

        setListAdapter(new MyAdapter());
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {

        class MyAdapter extends ArrayAdapter<TheLists>
        {
            MyAdapter()
            {
                super(ListPage.this, R.layout.list_row, R.id.sname, a);
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {
                LayoutInflater inflater = getLayoutInflater();
                View row = inflater.inflate(R.layout.list_row,parent,false);

                TextView name,category,id;

                name=(TextView)row.findViewById(R.id.sname);
                category=(TextView)row.findViewById(R.id.scategory);
                id=(TextView)row.findViewById(R.id.sid);
                name.setText(a[0].schemelist[position]);
                category.setText(a[0].categorylist[position]);
                id.setText(a[0].idlist[position]);
                return(row);
            }
        }
    }
}

我收到一个运行时错误,上面写着

05-02 18:56:32.224: E/AndroidRuntime(775): FATAL EXCEPTION: main
05-02 18:56:32.224: E/AndroidRuntime(775): java.lang.NullPointerException
05-02 18:56:32.224: E/AndroidRuntime(775):  at com.example.listinflator_demo.ListPage$MyAdapter.getView(ListPage.java:75)
05-02 18:56:32.224: E/AndroidRuntime(775):  at android.widget.AbsListView.obtainView(AbsListView.java:2159)

需要进行哪些更改?

1 个答案:

答案 0 :(得分:1)

在你的构造函数中,有一些非常奇怪的东西:

您将a作为参数传递给super。但是没有初始化,从来没有。你从哪里得到这个价值?

无论如何,我建议您更好地理解null pointer exception means。在使用java编程时,你会经常遇到这种错误,你会更好地理解如何自己找到并纠正它们,这比仅仅回答你的问题更有趣。