列表视图上的Android子项

列表视图上的Android子项,android,xml,android-listview,Android,Xml,Android Listview,我的问题:为什么我试图在subListView.setAdapter(adapter2)中将适配器设置到我的一个ListView时会出现java.lang.NullPointerException错误 我正在尝试创建一个用于学习目的的应用程序,该应用程序将在listview中显示主题,当单击listview上的一个项目/主题时,将显示一个子项目列表 现在我正试图显示一个listview视图,其中显示了它们的子项 这是我的密码: activity_main.xml <RelativeLayo

我的问题:为什么我试图在subListView.setAdapter(adapter2)中将适配器设置到我的一个ListView时会出现java.lang.NullPointerException错误

我正在尝试创建一个用于学习目的的应用程序,该应用程序将在listview中显示主题,当单击listview上的一个项目/主题时,将显示一个子项目列表

现在我正试图显示一个listview视图,其中显示了它们的子项

这是我的密码:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

<TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="150dp"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignRight="@+id/listView1"
    android:layout_below="@+id/button1"
    android:text="@string/hello_world" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textview1" >

</ListView>

</RelativeLayout>

group_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/groupItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ListView
    android:id="@+id/sublistView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/groupItem"
    android:layout_marginLeft="26dp" >
</ListView>

</RelativeLayout>

db_items.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:adjustViewBounds="true"
    android:maxHeight="60dp"
    android:maxWidth="60dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/subSubjectTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/imageView1"
    android:text="Jesus is God"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/subIdtextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Small Text"
    android:visibility="invisible"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/subScriptTextView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:text="John 1:1-12"
    android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    protected void onCreate(Bundle savedInstanceState) {
        //Creates DB and Tables if they do not exist... This part works
        popList();
    }

    public void popList(){
        ListView listView = (ListView) findViewById(R.id.listView1);
    ListView subListView = (ListView) findViewById(R.id.sublistView);
    TextView textv = (TextView)findViewById(R.id.textview1);
    String topList[] = {"First", "Second", "Third"};

        //Queries the DB and stores it in a Cursor... This part works

        textv.append(" Cursor Count = " + c.getCount()); //this is for debugging purposes

        int iId = c.getColumnIndex("id");
    int iSummary = c.getColumnIndex("Summary");
        int iScripts = c.getColumnIndex("Scripts");
    int iDescription = c.getColumnIndex("Description");
    int iSourceType = c.getColumnIndex("SourceType");

        cursor.moveToFirst();
        for (int j=0; j<c.getCount(); j++)
    {
        int image = 0;
        if(c.getString(iSourceType).equals("Bible"))
        {
            image = R.drawable.bible;
        }
        if(c.getString(iSourceType).equals("Article"))
        {
            image = R.drawable.article;
        }
        if(c.getString(iSourceType).equals("Video"))
        {
            image = R.drawable.video;
        }


        myLVItems.add(new lvItem(c.getString(iId), c.getString(iSummary), c.getString(iScripts), c.getString(iDescription), image));

        c.moveToNext();
    }
    ArrayAdapter<lvItem> adapter2 = new myListAdapter();

    textv.append(" subListView Count = " + myLVItems.size()+ " adapter2 Count: " + adapter2.getCount());   //this is for debugging purposes

    subListView.setAdapter(adapter2);  //this is first error that the logcat points to


    for (int i=0; i<topList.length; i++)
    {
        myGroupTopItems.add(new topgroupItem(topList[i], null));
    }

    ArrayAdapter<topgroupItem> adapter = new myTopListAdapter();

    listView.setAdapter(adapter);
    }

private class myListAdapter extends ArrayAdapter<lvItem>{
    public myListAdapter(){
        super(MainActivity.this, R.layout.db_items, myLVItems);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // make sure we have a view to work with (may have been given null
        View itemView = convertView;
        if(itemView == null)
        {
            itemView = getLayoutInflater().inflate(R.layout.db_items, parent, false);
        }

        //find the item to work with
        lvItem currentLVItem = myLVItems.get(position);

        //fill the view
        ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView1);
        imageView.setImageResource(currentLVItem.getIconId());

        TextView hiddenView = (TextView) itemView.findViewById(R.id.subIdtextView);
        hiddenView.setText(currentLVItem.getId());

        TextView summaryView = (TextView) itemView.findViewById(R.id.subSubjectTextView);
        summaryView.setText(currentLVItem.getSummary());

        TextView descripView = (TextView) itemView.findViewById(R.id.subScriptTextView2);
        descripView.setText(currentLVItem.getScripts());

        return itemView;
    }

}

private class myTopListAdapter extends ArrayAdapter<topgroupItem>{

    public myTopListAdapter() {
        super(MainActivity.this, R.layout.group_item, myGroupTopItems);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View itemView = convertView;

        if(itemView == null)
        {
            itemView = getLayoutInflater().inflate(R.layout.group_item, parent, false);
        }

        topgroupItem currentTGItem = myGroupTopItems.get(position);

        TextView textView = (TextView) itemView.findViewById(R.id.groupItem);
        textView.setText(currentTGItem.getText());

        return itemView;
    }

}
}
公共类MainActivity扩展活动{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
创建时受保护的void(Bundle savedInstanceState){
//如果数据库和表不存在,则创建它们…此部分有效
popList();
}
公共void popList(){
ListView ListView=(ListView)findViewById(R.id.listView1);
ListView subListView=(ListView)findViewById(R.id.subListView);
TextView textv=(TextView)findViewById(R.id.textview1);
字符串topList[]={“第一”、“第二”、“第三”};
//查询数据库并将其存储在游标中…这部分工作正常
textv.append(“Cursor Count=“+c.getCount());//这是为了调试目的
int iId=c.getColumnIndex(“id”);
int iSummary=c.getColumnIndex(“摘要”);
int iScripts=c.getColumnIndex(“脚本”);
int iDescription=c.getColumnIndex(“说明”);
int-iSourceType=c.getColumnIndex(“SourceType”);
cursor.moveToFirst();

对于(int j=0;j首先:在
onCreate()
中调用
popList()
。这是在列表填充项目之前,因此没有id为
sublistView
的子视图


第二:不要将列表添加到列表项中!在另一个ScrollView中没有ScrollView!如果您想拥有子列表,您可能想签出。

请发布堆栈跟踪。listview在listview中?为什么不改为使用expandablelistview?expandablelistview是一种方式!谢谢