ListActivity,ListView和ListAdapter崩溃Android应用程序

时间:2012-03-20 15:16:53

标签: android android-listview listactivity listadapter

我在创建简单列表时遇到了麻烦(一些可扩展列表已经在运行)。当我setListAdapter()时,应用程序崩溃了。我的错误在哪里?

Acivity:

public class UserListActivity extends ListActivity {

    UserListAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.userlist);
        mAdapter = new UserListAdapter(this);
        setListAdapter(mAdapter);
        registerForContextMenu(getListView());
    }
}

适配器:

public class UserListAdapter implements ListAdapter {

    private Context context;
    private List<UserItem> users;

    public UserListAdapter(Context c) {

        context = c;
        users = new Database(context).getAllUsers(); // this class is working properly
    }

    public int getCount() {

        return users.size();
    }

    public Object getItem(int pos) {

        return users.get(pos).getSimplified();
    }

    public long getItemId(int pos) {

        return users.get(pos).getId();
    }

    public int getItemViewType(int arg0) {

        return 0;
    }

    public View getView(int arg0, View arg1, ViewGroup arg2) {

        return null;
    }

    public int getViewTypeCount() {

        return 0;
    }

    public boolean hasStableIds() {

        return true;
    }

    public boolean isEmpty() {

        return users.size() == 0;
    }

    public void registerDataSetObserver(DataSetObserver arg0) {

    }

    public void unregisterDataSetObserver(DataSetObserver arg0) {

    }

    public boolean areAllItemsEnabled() {

        return false;
    }

    public boolean isEnabled(int arg0) {

        return false;
    }
}

查看:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#312843" >

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="fill_parent"
        android:layout_height="82dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:scaleType="fitXY"
        android:src="@drawable/header_side" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="235dp"
        android:layout_height="82dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/header" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView2"
        android:background="#312843"
        android:cacheColorHint="#312843"
        android:fadeScrollbars="true" >
    </ListView>

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

您的getView()方法必须在您设置时返回View而不是null。 (这是一个如何做的教程:http://www.vogella.de/articles/AndroidListView/article.html#listview_adapterown

此外,getViewTypeCount()方法必须返回不同于0的值。

   public int getViewTypeCount() {
        return 1;
    }

答案 1 :(得分:1)

这是你的问题

  public View getView(int arg0, View arg1, ViewGroup arg2) { 

        return null; 
    }

你不能返回空视图,它会给你NPE并且应用程序会崩溃

in getView you gotta do something like this
 public View getView(int arg0, View convertView, ViewGroup arg2) { 
        View view = convertView;
        if (convertView == null) {
            LayoutInflater layout = LayoutInflater.from(context);
                              view = layout.inflate(R.layout.MyLayout, null);
        }
return view;
}