###前提・実現したいこと
Androidアプリで、ExpandableListViewを実装したいです。
###発生している問題・エラーメッセージ
.setAdapterメソッドでアプリが強制終了してしまいます。
###該当のソースコード
Java
1public class ExListTLActivity extends Activity { 2 3 private Context context; 4 5 // アダプター作成 6 private CustomExpandableListAdapter adapterExpandableList; 7 //private ExpandableListAdapter adapterExpandableList; 8 //private SimpleExpandableListAdapter adapterExpandableList; 9 10 11 private ExpandableListView expandListView; 12 13 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 19 context = this; 20 21 setContentView(R.layout.activity_exlist_tl); 22 23 //showToast(findViewById(R.id.Exlist).toString()); 24 // 定義しているExpandableListViewを取得 25 //expandListView = new ExpandableListView(this); 26 expandListView = (ExpandableListView) findViewById(R.id.Exlist); 27 // アダプターをリストにセット 28 //キャストしないとアプリが落ちる。。。 29 adapterExpandableList = (CustomExpandableListAdapter)new CustomExpandableListAdapter(this); 30 31 //20160816現在 ここでアプリ落ちてしまう・・・ 32 expandListView.setAdapter((CustomExpandableListAdapter)adapterExpandableList); 33 //expandListView.setAdapter((BaseExpandableListAdapter)adapterExpandableList); 34 //expandListView.setAdapter((ExpandableListAdapter)adapterExpandableList); 35 //expandListView.setAdapter(adapterExpandableList); 36 37 38 } 39} 40 41class CustomExpandableListAdapter extends BaseExpandableListAdapter { 42//class CustomExpandableListAdapter extends SimpleExpandableListAdapter { 43 /** 親アイテムのリスト */ 44 private List<twitter4j.Status> groups; 45 /** 子アイテムリストのリスト */ 46 private List<List<twitter4j.Status>> children; 47 48 private Context context; 49 private LayoutInflater mInflater; 50 51 public CustomExpandableListAdapter(Context context) { 52 this.context = context; 53 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 54 } 55 56 //以下はBaseExpandableListAdapterを継承するときに必要 57 @Override 58 public int getGroupCount() { 59 return children.size(); 60 } 61 62 @Override 63 public int getChildrenCount(int groupPosition) { 64 return children.get(groupPosition).size(); 65 } 66 67 @Override 68 public Object getGroup(int groupPosition) { 69 return groups.get(groupPosition); 70 } 71 72 @Override 73 public Object getChild(int groupPosition, int childPosition) { 74 return children.get(groupPosition).get(childPosition); 75 } 76 77 @Override 78 public long getGroupId(int groupPosition) { 79 return groupPosition; 80 } 81 82 @Override 83 public long getChildId(int groupPosition, int childPosition) { 84 return childPosition; 85 } 86 87 @Override 88 public boolean hasStableIds() { 89 return true; 90 } 91 92 93 /** ビューに変化があった時に呼ばれる */ 94 /** 95 * 親アイテムの取得 96 */ 97 @Override 98 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 99 ViewGroup parent) { 100 101 if (convertView == null) { 102 convertView = mInflater.inflate(R.layout.list_item_tweet_parent, null); 103 }else{ 104 105 } 106 107 return convertView; 108 109 } 110 111 /** 112 * 子アイテムの取得 113 */ 114 @Override 115 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 116 View convertView, ViewGroup parent) { 117 118 if (convertView == null) { 119 convertView = mInflater.inflate(R.layout.list_item_tweet_child, null); 120 }else{ 121 122 } 123 124 return convertView; 125 126 } 127 128 @Override 129 public boolean isChildSelectable(int groupPosition, int childPosition) { 130 return true; 131 } 132 133} 134 135
###試したこと
使用するアダプターの型を
ExpandableListAdapter、SimpleExpandableListAdapter、BaseExpandableListAdapter
などいろいろ変えてみましたが、解決しませんでした。
###実行環境
Android 5.0.2
ASUS ZenPad 7.0 Z370C
開発PCはスペックが悪いのか、Androidエミュレータが満足に動かない状態です。
###Logを取ってみたところ
起動時に問題のコードを強制的に通るようにして、エミュレータからLogを取ってみたところ
08-16 17:00:48.214: E/AndroidRuntime(2108): FATAL EXCEPTION: main
08-16 17:00:48.214: E/AndroidRuntime(2108): Process: com.example.test3, PID: 2108
08-16 17:00:48.214: E/AndroidRuntime(2108): java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
08-16 17:00:48.214: E/AndroidRuntime(2108): at com.example.test3.CustomExpandableListAdapter.getGroupCount(ExListTLActivity.java:161)
setAdapterの時点でgetGroupCount()が実行されてしまい
nullの要素数を返そうとして落ちてしまうようです。
###解決方法
swordoneさんの回答の2番目のとおりでした。
class CustomExpandableListAdapter 内の
private List<twitter4j.Status> groups; private List<List<twitter4j.Status>> children;
の部分を
private List<twitter4j.Status> groups = new ArrayList<twitter4j.Status>(); private List<List<twitter4j.Status>> children = new ArrayList<List<twitter4j.Status>>();
にしたところ、アプリが落ちなくなりました。
ありがとうございました。
回答1件
あなたの回答
tips
プレビュー