可扩展列表适配器的问题

时间:2011-02-14 13:38:33

标签: android list expandable

我是Android开发的新手,所以我希望有人可以帮我解决这个问题。

我正在尝试创建一个可扩展列表,我尝试使用谷歌搜索,并阅读谷歌文档。但不知何故,我无法绕过它。我的代码在eclipse中没有错误。但是当它在模拟器中运行时,它会在启动时崩溃。当我将 适配器 设置为 视图 时,我认为我做错了。

这是我的代码:

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这里是logcat输出:

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 

1 个答案:

答案 0 :(得分:4)

我明白了。对于任何感兴趣的人,您所要做的就是将“Context”声明放在“MyExpandableListAdapter”中,并将“this”添加到适配器声明中。请参阅下面的代码:

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;
        }
    }
}