I am new to Android development, so I hope someone can help me with this problem.
I am trying to create a expandable list, I have tried googling, and read through googles documentation. But somehow I can not wrap my head around it. My code gives no errors in eclipse. But when running it in the emulator, it crashes on startup. I think I am doing something wrong when I set the adapter to the view.
Here is my code:
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ExpandableListView;
import android.widget.ExpandableListAdapter;
import android.widget.BaseExpandableListAdapter;
import android.view.LayoutInflater;
import android.content.Context;
public class hmenyActivity extends MyActivity {
ExpandableListAdapter adapter;
private Context context;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hmeny);
adapter = new MyExpandableListAdapter();
ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
listView.setOnChildClickListener(new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4)
{
Toast.makeText(getBaseContext(), "Child clicked", Toast.LENGTH_LONG).show();
return false;
}
});
listView.setAdapter(adapter);
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private String[] groups = {
"Test1",
"Test2",
"Test3"
};
private String[][] children = {
{
"foo1",
"foo2",
"foo3"
},
{
"foo1",
"foo2",
"foo3"
},
{
"foo1",
"foo2",
"foo3"
}
};
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.underkat_layout, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.tvChild);
textView.setText(getChild(groupPosition, childPosition).toString());
return convertView;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.kat_layout, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.tvGroup);
textView.setText(getGroup(groupPosition).toString());
return convertView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
}
@ingo here is the logcat output:
02-14 14:49:26.673: ERROR/AndroidRuntime(302): Uncaught handler: thread main exiting due to uncaught exception
02-14 14:49:26.693: ERROR/AndroidRuntime(302): java.lang.NullPointerException
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at com.android.quizapp.hmenyActivity$MyExpandableListAdapter.getGroupView(hmenyActivity.java:151)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.AbsListView.obtainView(AbsListView.java:1273)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.ListView.measureHeightOfChildren(ListView.java:1147)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.ListView.onMeasure(ListView.java:1060)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.View.measure(View.java:7703)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.View.measure(View.java:7703)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.View.measure(View.java:7703)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.View.measure(View.java:7703)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.View.measure(View.java:7703)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.ViewRoot.performTraversals(ViewRoot.java:747)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.os.Looper.loop(Looper.java:123)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at android.app.ActivityThread.main(ActivityThread.java:4203)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at java.lang.reflect.Method.invoke(Method.java:521)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
02-14 14:49:26.693: ERROR/AndroidRuntime(302): at dalvik.system.NativeStart.main(Native Method)
02-14 14:49:26.713: INFO/Process(51): Sending signal. PID: 302 SIG: 3
02-14 14:49:26.713: INFO/dalvikvm(302): threadid=7: reacting to signal 3
02-14 14:49:26.713: ERROR/dalvikvm(302): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
02-14 14:49:36.493: WARN/ActivityManager(51): Launch timeout has expired, giving up wake lock!
02-14 14:49:36.506: WARN/ActivityManager(51): Activity idle timeout for HistoryRecord{43919b58 com.android.skiquiz/.hmenyActivity}
02-14 14:49:41.533: WARN/ActivityManager(51): Activity destroy
I figured it out. For anyone interested, all you have to do is put the "Context" declaration inside "MyExpandableListAdapter", and add "this" to the adapter declaration. See my code below:
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ExpandableListView;
import android.widget.ExpandableListAdapter;
import android.widget.BaseExpandableListAdapter;
import android.view.LayoutInflater;
import android.content.Context;
public class hmenyActivity extends MyActivity {
ExpandableListAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hmeny);
adapter = new MyExpandableListAdapter(this);
ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
listView.setOnChildClickListener(new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4)
{
Toast.makeText(getBaseContext(), "Child clicked", Toast.LENGTH_LONG).show();
return false;
}
});
listView.setAdapter(adapter);
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private String[] groups = {
"Test1",
"Test2",
"Test3"
};
private String[][] children = {
{
"foo1",
"foo2",
"foo3"
},
{
"foo1",
"foo2",
"foo3"
},
{
"foo1",
"foo2",
"foo3"
}
};
//Add these lines to your code
private Context context;
public MyExpandableListAdapter(Context context) {
this.context = context;
}
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.underkat_layout, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.tvChild);
textView.setText(getChild(groupPosition, childPosition).toString());
return convertView;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.kat_layout, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.tvGroup);
textView.setText(getGroup(groupPosition).toString());
return convertView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
}