ExpandableListView在“setAdapter”崩溃

时间:2015-12-09 16:53:53

标签: android android-fragments android-listview android-adapter

运行时错误:java.lang.RuntimeException:对于ExpandableListView,使用setAdapter(ExpandableListAdapter)而不是setAdapter(ListAdapter)

但我正在调用setAdapter(ExpandableListAdapter),因为我的适配器扩展了BaseExpandableListAdapter。有谁知道可能是什么问题?

适配器:

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

Context mContext;

public MyExpandableListAdapter(Context context){
    this.mContext = context;
}

// Sample data set. children[i] contains the children (String[]) for
// groups[i].
private String[] groups = { "GroupItems 0", "GroupItems 1",
        "GroupItems 2", "GroupItems 3" };
private String[][] children = {
        { "SubItem 0", "SubItem 1", "SubItem 2", "SubItem 3" },
        { "SubItem 0", "SubItem 1", "SubItem 2", "SubItem 3" },
        { "SubItem 0", "SubItem 1" },
        { "SubItem 0", "SubItem 1", "SubItem 2", } };

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 TextView getGenericView() {
    // Layout parameters for the ExpandableListView
    AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, 64);

    TextView textView = new TextView(mContext);
    textView.setLayoutParams(lp);
    // Center the text vertically
    textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
    // Set the text starting position
    textView.setPadding(36, 0, 0, 0);
    return textView;
}

public View getChildView(int groupPosition, int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    TextView textView = getGenericView();
    textView.setText(getChild(groupPosition, childPosition).toString());
    return textView;
}


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) {
    TextView textView = getGenericView();
    textView.setText(getGroup(groupPosition).toString());
    return textView;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

public boolean hasStableIds() {
    return true;
}
}

片段:

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

public class PickTimeFragment extends Fragment {

public PickTimeFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_pick_time, container, false);
    ExpandableListView elv = (ExpandableListView) view.findViewById(R.id.expandableListView);
    MyExpandableListAdapter listAdapter = new MyExpandableListAdapter(getActivity());
    elv.setAdapter(listAdapter);
    return view;
}

logcat的:

12-08 16:29:32.018 4081-4081/? **E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yarinf.playtime/com.example.yarinf.playtime.PickTime}: android.view.InflateException: Binary XML file line #14: Error inflating class <unknown>**
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5221)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-08 16:29:32.018 4081-4081/? **E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class <unknown>**
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.view.LayoutInflater.createView(LayoutInflater.java:633)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at com.example.yarinf.playtime.PickTimeFragment.onCreateView(PickTimeFragment.java:20)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.Fragment.performCreateView(Fragment.java:2053)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.BackStackRecord.run(BackStackRecord.java:833)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.Activity.performStart(Activity.java:5952)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:144) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5221) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
12-08 16:29:32.018 4081-4081/? **E/AndroidRuntime:  Caused by: java.lang.RuntimeException: For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of setAdapter(ListAdapter)**
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:556)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.widget.ListView.<init>(ListView.java:159)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.widget.ExpandableListView.<init>(ExpandableListView.java:236)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.widget.ExpandableListView.<init>(ExpandableListView.java:231)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.widget.ExpandableListView.<init>(ExpandableListView.java:227)
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at java.lang.reflect.Constructor.newInstance(Native Method) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at com.example.yarinf.playtime.PickTimeFragment.onCreateView(PickTimeFragment.java:20) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.Fragment.performCreateView(Fragment.java:2053) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.BackStackRecord.run(BackStackRecord.java:833) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.Activity.performStart(Activity.java:5952) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:144) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5221) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
12-08 16:29:32.018 4081-4081/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

0 个答案:

没有答案