Android ListView NullpointerException问题

Android ListView NullpointerException问题,android,listview,nullpointerexception,Android,Listview,Nullpointerexception,我尝试创建一个XML文件,该文件采用一个简单的ListView,并设置了一个个人适配器,如下所示: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"

我尝试创建一个XML文件,该文件采用一个简单的ListView,并设置了一个个人适配器,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <ListView
        android:id="@+id/listpromo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>
请告诉我怎么做

这是我的独立适配器:

public class SeparatedListAdapter extends BaseAdapter 
{
    public final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>();
    public final ArrayAdapter<String> headers;
    public final static int TYPE_SECTION_HEADER = 0;

    public SeparatedListAdapter(Context context) 
    {
        headers = new ArrayAdapter<String>(context, R.layout.list_header_category3);
    }

    public void addSection(String section, Adapter adapter) 
    {
        this.headers.add(section);
        this.sections.put(section, adapter);
    }

    public Object getItem(int position) 
    {
        for(Object section : this.sections.keySet()) 
        {
            Adapter adapter = sections.get(section);
            int size = adapter.getCount() + 1;

            // check if position inside this section 
            if(position == 0) return section;
            if(position < size) return adapter.getItem(position - 1);

            // otherwise jump into next section
            position -= size;
        }
        return null;
    }

    public int getCount() 
    {
        // total together all sections, plus one for each section header
        int total = 0;
        for(Adapter adapter : this.sections.values())
            total += adapter.getCount() + 1;
        return total;
    }

    public int getViewTypeCount() 
    {
        // assume that headers count as one, then total all sections
        int total = 1;
        for(Adapter adapter : this.sections.values())
            total += adapter.getViewTypeCount();
        return total;
    }

    public int getItemViewType(int position) 
    {
        int type = 1;
        for(Object section : this.sections.keySet()) 
        {
            Adapter adapter = sections.get(section);
            int size = adapter.getCount() + 1;

            // check if position inside this section 
            if(position == 0) return TYPE_SECTION_HEADER;
            if(position < size) return type + adapter.getItemViewType(position - 1);

            // otherwise jump into next section
            position -= size;
            type += adapter.getViewTypeCount();
        }
        return -1;
    }

    public boolean areAllItemsSelectable() 
    {
        return false;
    }

    public boolean isEnabled(int position) 
    {
        return (getItemViewType(position) != TYPE_SECTION_HEADER);
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        int sectionnum = 0;
        for(Object section : this.sections.keySet()) {
            Adapter adapter = sections.get(section);
            int size = adapter.getCount() + 1;

            // check if position inside this section 
            if(position == 0) return headers.getView(sectionnum, convertView, parent);
            if(position < size) return adapter.getView(position - 1, convertView, parent);

            // otherwise jump into next section
            position -= size;
            sectionnum++;
        }
        return null;
    }

    public long getItemId(int position) 
    {
        return position;
    }
}
public类SeparatedListAdapter扩展了BaseAdapter
{
公共最终映射节=新建LinkedHashMap();
公共最终阵列适配器标头;
公共最终静态整型\节\头=0;
公共分隔的ListAdapter(上下文)
{
headers=新的ArrayAdapter(上下文,R.layout.list_header_category3);
}
public void addSection(字符串段、适配器)
{
此.headers.add(节);
此.sections.put(节,适配器);
}
公共对象getItem(int位置)
{
for(对象节:this.sections.keySet())
{
适配器=节。获取(节);
int size=adapter.getCount()+1;
//检查此部分内的位置是否正确
如果(位置==0)返回段;
if(位置<大小)返回适配器.getItem(位置-1);
//否则跳到下一节
位置-=大小;
}
返回null;
}
public int getCount()
{
//所有章节合计,每个章节标题加上一个章节
int-total=0;
对于(适配器:this.sections.values())
总计+=适配器.getCount()+1;
返回总数;
}
public int getViewTypeCount()
{
//假设头数为一,然后合计所有部分
整数合计=1;
对于(适配器:this.sections.values())
总计+=适配器。getViewTypeCount();
返回总数;
}
public int getItemViewType(int位置)
{
int型=1;
for(对象节:this.sections.keySet())
{
适配器=节。获取(节);
int size=adapter.getCount()+1;
//检查此部分内的位置是否正确
if(position==0)返回类型\u节\u头;
if(位置<大小)返回类型+适配器.getItemViewType(位置-1);
//否则跳到下一节
位置-=大小;
类型+=适配器。getViewTypeCount();
}
返回-1;
}
公共布尔值areAllItemsSelectable()
{
返回false;
}
公共布尔值isEnabled(整型位置)
{
返回(getItemViewType(位置)!=类型\节\标题);
}
公共视图getView(int位置、视图转换视图、视图组父视图)
{
int sectionnum=0;
for(对象节:this.sections.keySet()){
适配器=节。获取(节);
int size=adapter.getCount()+1;
//检查此部分内的位置是否正确
if(position==0)返回headers.getView(sectionnum,convertView,parent);
if(position
如果不设置contentView,就不能说
findViewById()
。您必须设置包含
列表视图的XML布局

setContentView(R.layout.yourlayout)
。否则,您的
列表视图列表将为空

因此,在您的
onCreate()
中:

并删除
onCreate()底部的这一行:


你能发布完整的LogCat例外情况吗?我们可能需要下面的“由…引起”不管是什么,它都是由
ListSample\u category3.java:line 77
中的空指针引起的。它可能是add_节()之一。你能告诉我哪一行是第77行吗?ListSample_类别3上的第77行是这一行:list.setOnItemClickListener(listener);但是如果我删除eventlistener,问题现在出现在第80行,它是:list.setAdapter(adapter);当我这样使用列表视图时:ListView List=newlistview(这个);它可以工作,但我不能在上面添加按钮,所以它不好。那没关系。只需设置contentView。这是一个XML布局文件,它的listView id为
listpromo
。包含ListView的
    08-05 17:00:26.304: E/AndroidRuntime(3226): FATAL EXCEPTION: main
08-05 17:00:26.304: E/AndroidRuntime(3226): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dev.prixo/com.dev.prixo.OngletsActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dev.prixo/com.dev.prixo.ListSample_category3}: java.lang.NullPointerException
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.os.Looper.loop(Looper.java:130)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.app.ActivityThread.main(ActivityThread.java:3687)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at java.lang.reflect.Method.invokeNative(Native Method)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at java.lang.reflect.Method.invoke(Method.java:507)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at dalvik.system.NativeStart.main(Native Method)
08-05 17:00:26.304: E/AndroidRuntime(3226): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dev.prixo/com.dev.prixo.ListSample_category3}: java.lang.NullPointerException
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1491)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:676)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.widget.TabHost.setCurrentTab(TabHost.java:348)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.widget.TabHost.addTab(TabHost.java:227)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at com.dev.prixo.OngletsActivity.onCreate(OngletsActivity.java:34)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-05 17:00:26.304: E/AndroidRuntime(3226):     ... 11 more
08-05 17:00:26.304: E/AndroidRuntime(3226): Caused by: java.lang.NullPointerException
08-05 17:00:26.304: E/AndroidRuntime(3226):     at com.dev.prixo.ListSample_category3.onCreate(ListSample_category3.java:77)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-05 17:00:26.304: E/AndroidRuntime(3226):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-05 17:00:26.304: E/AndroidRuntime(3226):     ... 20 more
public class SeparatedListAdapter extends BaseAdapter 
{
    public final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>();
    public final ArrayAdapter<String> headers;
    public final static int TYPE_SECTION_HEADER = 0;

    public SeparatedListAdapter(Context context) 
    {
        headers = new ArrayAdapter<String>(context, R.layout.list_header_category3);
    }

    public void addSection(String section, Adapter adapter) 
    {
        this.headers.add(section);
        this.sections.put(section, adapter);
    }

    public Object getItem(int position) 
    {
        for(Object section : this.sections.keySet()) 
        {
            Adapter adapter = sections.get(section);
            int size = adapter.getCount() + 1;

            // check if position inside this section 
            if(position == 0) return section;
            if(position < size) return adapter.getItem(position - 1);

            // otherwise jump into next section
            position -= size;
        }
        return null;
    }

    public int getCount() 
    {
        // total together all sections, plus one for each section header
        int total = 0;
        for(Adapter adapter : this.sections.values())
            total += adapter.getCount() + 1;
        return total;
    }

    public int getViewTypeCount() 
    {
        // assume that headers count as one, then total all sections
        int total = 1;
        for(Adapter adapter : this.sections.values())
            total += adapter.getViewTypeCount();
        return total;
    }

    public int getItemViewType(int position) 
    {
        int type = 1;
        for(Object section : this.sections.keySet()) 
        {
            Adapter adapter = sections.get(section);
            int size = adapter.getCount() + 1;

            // check if position inside this section 
            if(position == 0) return TYPE_SECTION_HEADER;
            if(position < size) return type + adapter.getItemViewType(position - 1);

            // otherwise jump into next section
            position -= size;
            type += adapter.getViewTypeCount();
        }
        return -1;
    }

    public boolean areAllItemsSelectable() 
    {
        return false;
    }

    public boolean isEnabled(int position) 
    {
        return (getItemViewType(position) != TYPE_SECTION_HEADER);
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        int sectionnum = 0;
        for(Object section : this.sections.keySet()) {
            Adapter adapter = sections.get(section);
            int size = adapter.getCount() + 1;

            // check if position inside this section 
            if(position == 0) return headers.getView(sectionnum, convertView, parent);
            if(position < size) return adapter.getView(position - 1, convertView, parent);

            // otherwise jump into next section
            position -= size;
            sectionnum++;
        }
        return null;
    }

    public long getItemId(int position) 
    {
        return position;
    }
}
protected void onCreate(Bundle bundle) {
     super.onCreate(bundle);
     setContentView(R.layout.yourlayout);
}
setContentView(listview);