2013-12-17 3 views
2

Мой вопрос: Почему я получаю сообщение об ошибке java.lang.NullPointerException для установки набора адаптера в один из моих списков в subListView.setAdapter (adapter2);Android Subitems в 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; 
    } 

} 
} 

Это то, что textv дисплеи: Cursir Count = 3 subListView Count = 3 adapter2 Count: 3

Я не знаю, почему я получаю Null для subListView. Я не ищу расширенный список с тем, что я делаю

Извините за весь код. Любая помощь была бы принята с благодарностью

+0

Опубликовать стол следы пожалуйста. – Emmanuel

+0

listview в виде списка? почему бы не использовать expandablelistview? – njzk2

ответ

1

Первый: вы вызываете popList() в onCreate(). Это до того, как List заполнится элементами, и поэтому нет дочернего представления с идентификатором sublistView.

Второе: НЕ ДОБАВЛЯЙТЕ СПИСОК В СПИСОК СПИСКОВ! Нет ScrollViews внутри другого ScrollView! Возможно, вы захотите проверить ExpandableListView, если вы хотите иметь вспомогательные списки.

+0

ExpandableListView - это путь! благодаря – user908759

Смежные вопросы