Android:setAdapter()获取空指针异常

时间:2013-07-27 12:35:34

标签: android nullpointerexception

我是Android编程的新手。我正在尝试创建一个基本列表应用程序。我在使用setAdapter()设置适配器时发现nullpointer异常。我的代码如下所示。

ListAdapter.java

package com.example.listexmpl;
import java.util.ArrayList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter{

    private ArrayList<ListRecord> records = new ArrayList<ListRecord>();


    public ListAdapter(){
        records.add(new ListRecord("Rajat","I'm feeling very happy today."));
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return records.size();
    }

    @Override
    public Object getItem(int index) {
        // TODO Auto-generated method stub
        return getItem(index);
    }

    @Override
    public long getItemId(int index) {
        // TODO Auto-generated method stub
        return index;
    }

    @Override
    public View getView(int index, View view, ViewGroup parent) {
        // TODO Auto-generated method stub
        if(view==null){
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            view = inflater.inflate(R.layout.list_record,parent,false);
        }
        ListRecord listr = records.get(index);
        TextView nameView = (TextView) view.findViewById(R.id.name_view);
        TextView statView = (TextView) view.findViewById(R.id.stat_view);
        nameView.setText(listr.getName());
        statView.setText(listr.getStatus());
        return view;
    }

}

ListActivity.java

package com.example.listexmpl;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;

public class ListActivity extends Activity {

    ListAdapter lstrecordAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        ListView lstview = null;
        lstview = (ListView) findViewById(R.id.list_record);
        lstrecordAdapter = new ListAdapter();
        lstview.setAdapter(lstrecordAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_list, menu);
        return true;
    }

}

当我在try-catch块中放入包含setAdapter()的语句时,应用程序会运行,但除了应用程序标题之外,屏幕上没有任何内容可以看到。我哪里出错了?

[编辑] activity_list.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

</ListView>

1 个答案:

答案 0 :(得分:3)

您错过了ListView的ID项目

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/listviewid"
    >

</ListView>

活动内部更改

ListView lstview = null;
lstview = (ListView) findViewById(R.id.list_record);

 ListView lstview = null;
 lstview = (ListView) findViewById(R.id.listviewid);