我的问题:为什么我在java.lang.NullPointerException中遇到错误,试图将一个适配器设置为我在subListView.setAdapter(adapter2)中的一个listviews;
我正在尝试创建一个用于学习目的的应用程序,该应用程序将在列表视图中显示主题,当单击列表视图中的某个项目/主题时,将显示子项目列表。
现在我只是想显示一个列表视图,并显示其子项目。
这是我的代码:
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;
}
}
}
这就是textv显示的内容:Cursir Count = 3 subListView Count = 3 adapter2 Count:3
我不知道为什么我为subListView获取Null。我不是在寻找一个扩展的列表视图,我正在做什么
对不起所有代码。任何帮助将不胜感激
答案 0 :(得分:1)
首先:您在popList()
中致电onCreate()
。这是在List填充项目之前,因此没有名为sublistView
的子视图。
第二:请勿将列表添加到列表项中!在另一个ScrollView中没有ScrollViews!如果您想要子列表,可能需要查看ExpandableListView。