0

我对 Android 编程非常陌生。我正在尝试创建一个基本的列表应用程序。我在使用 setAdapter() 设置适配器时遇到空指针异常。我的代码如下。

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

}

当我将包含 setAdapter() 的语句放入 try-catch 块时,应用程序会运行,但屏幕上除了应用程序标头外什么都看不到。我哪里出错了?

[编辑] activity_list.xml

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

</ListView>
4

1 回答 1

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);
于 2013-07-27T12:45:01.997 回答