1
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)

需要什么改变?

4

1 回答 1

1

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

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

无论如何,我建议你更好地理解想要一个空指针异常的意思。在java中编程时你会经常遇到这种错误,你会更好地理解如何自己找到并纠正它们,这比仅仅回答你的问题更有趣。

于 2013-05-02T19:37:23.787 回答