与扩展列表适配器问题

与扩展列表适配器问题

本文介绍了与扩展列表适配器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid开发,所以我希望有人能帮助我解决这个问题。

我想创建一个扩展列表,我曾尝试谷歌上搜索,并通读谷歌文档。但不知何故,我不能完成我的头周围。我的code给出任何错误的日食。但在模拟器中运行时,它崩溃上启动。我想我做错了什么,当我将适配器视图

下面是我的code:

 进口android.os.Bundle;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.TextView;
进口android.widget.ExpandableListView;
进口android.widget.ExpandableListAdapter;
进口android.widget.BaseExpandableListAdapter;
进口android.view.LayoutInflater;
进口android.content.Context;

公共类hmenyActivity扩展MyActivity {

    ExpandableListAdapter适配器;

    私人上下文的背景下;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.hmeny);

        适配器=新MyExpandableListAdapter();

        ExpandableListView的ListView =(ExpandableListView)findViewById(R.id.listView);

        listView.setOnChildClickListener(新OnChildClickListener()
        {
            @覆盖
            公共布尔onChildClick(ExpandableListView为arg0,查看ARG1,INT ARG2,诠释ARG3,长ARG4)
            {
            Toast.makeText(getBaseContext(),儿童点击,Toast.LENGTH_LONG).show();
            返回false;
            }

        });


        listView.setAdapter(适配器);

    }

    公共类MyExpandableListAdapter扩展BaseExpandableListAdapter {

        私有String []组= {
                测试1,
                Test2的,
                Test3的
            };

        私有String [] []孩子= {

                {
                    foo1
                    foo2的,
                    foo3
                },

                {
                    foo1
                    foo2的,
                    foo3
                },

                {
                    foo1
                    foo2的,
                    foo3
                }
            };

        公共对象getChild(INT groupPosition,诠释childPosition){
            返回儿[groupPosition] [childPosition]
        }

        众长getChildId(INT groupPosition,诠释childPosition){
            返回childPosition;
        }

        公众诠释getChildrenCount(INT groupPosition){
            返回儿[groupPosition] .length;
        }

        公共查看getChildView(INT groupPosition,诠释childPosition,布尔isLastChild,
                查看convertView,ViewGroup中父){

            如果(convertView == NULL){
                LayoutInflater infalInflater =(LayoutInflater)上下文
                .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());
            返回convertView;
        }

        公共对象getGroup(INT groupPosition){
            返回组[groupPosition]
        }

        公众诠释getGroupCount(){
                返回groups.length;
        }

        众长getGroupId(INT groupPosition){
            返回groupPosition;
        }

        公共查看getGroupView(INT groupPosition,布尔isExpanded,查看convertView,
                ViewGroup中父){
            如果(convertView == NULL){
                LayoutInflater infalInflater =(LayoutInflater)上下文
                .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());
            返回convertView;
        }

        公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
            返回true;
        }

        公共布尔hasStableIds(){
            返回true;
        }
    }
}
 

@ingo这里是logcat的输出:

  02-14 14:49:26.673:ERROR / AndroidRuntime(302):未捕获的处理程序:螺纹主力退出,由于未捕获的异常
02-14 14:49:26.693:ERROR / AndroidRuntime(302):显示java.lang.NullPointerException
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在com.android.quizapp.hmenyActivity $ MyExpandableListAdapter.getGroupView(hmenyActivity.java:151)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.widget.AbsListView.obtainView(AbsListView.java:1273)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.widget.ListView.measureHeightOfChildren(ListView.java:1147)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.widget.ListView.onMeasure(ListView.java:1060)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.view.View.measure(View.java:7703)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.view.View.measure(View.java:7703)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.view.View.measure(View.java:7703)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.view.View.measure(View.java:7703)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.view.View.measure(View.java:7703)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.view.ViewRoot.performTraversals(ViewRoot.java:747)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.view.ViewRoot.handleMessage(ViewRoot.java:1613)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.os.Handler.dispatchMessage(Handler.java:99)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.os.Looper.loop(Looper.java:123)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在android.app.ActivityThread.main(ActivityThread.java:4203)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在java.lang.reflect.Method.invokeNative(本机方法)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在java.lang.reflect.Method.invoke(Method.java:521)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:791)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
02-14 14:49:26.693:ERROR / AndroidRuntime(302):在dalvik.system.NativeStart.main(本机方法)
02-14 14:49:26.713:信息/工艺(51):发送信号。 PID:302 SIG:3
02-14 14:49:26.713:信息/ dalvikvm(302):主题ID = 7:反应以信号3
02-14 14:49:26.713:ERROR / dalvikvm(302):无法打开堆栈跟踪文件/data/anr/traces.txt':权限被拒绝
02-14 14:49:36.493:WARN / ActivityManager(51):启动超时已过期,放弃唤醒锁!
02-14 14:49:36.506:WARN / ActivityManager(51):活动空闲超时时间为HistoryRecord {43919b58 com.android.skiquiz / .hmenyActivity}
02-14 14:49:41.533:WARN / ActivityManager(51):活动破坏
 

解决方案

我想它了。对于任何有兴趣,你所要做的就是把背景声明中MyExpandableListAdapter,并增加本到适配器声明。请看下文中我code:

 进口android.os.Bundle;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.TextView;
进口android.widget.ExpandableListView;
进口android.widget.ExpandableListAdapter;
进口android.widget.BaseExpandableListAdapter;
进口android.view.LayoutInflater;
进口android.content.Context;

公共类hmenyActivity扩展MyActivity {

    ExpandableListAdapter适配器;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.hmeny);

        适配器=新MyExpandableListAdapter(本);

        ExpandableListView的ListView =(ExpandableListView)findViewById(R.id.listView);

        listView.setOnChildClickListener(新OnChildClickListener()
        {
            @覆盖
            公共布尔onChildClick(ExpandableListView为arg0,查看ARG1,INT ARG2,诠释ARG3,长ARG4)
            {
            Toast.makeText(getBaseContext(),儿童点击,Toast.LENGTH_LONG).show();
            返回false;
            }

        });


        listView.setAdapter(适配器);

    }

    公共类MyExpandableListAdapter扩展BaseExpandableListAdapter {

        私有String []组= {
                测试1,
                Test2的,
                Test3的
            };

        私有String [] []孩子= {

                {
                    foo1
                    foo2的,
                    foo3
                },

                {
                    foo1
                    foo2的,
                    foo3
                },

                {
                    foo1
                    foo2的,
                    foo3
                }
            };

    //添加这些行到你的code
    私人上下文的背景下;

    公共MyExpandableListAdapter(上下文的背景下){
        this.context =背景;
    }

        公共对象getChild(INT groupPosition,诠释childPosition){
            返回儿[groupPosition] [childPosition]
        }

        众长getChildId(INT groupPosition,诠释childPosition){
            返回childPosition;
        }

        公众诠释getChildrenCount(INT groupPosition){
            返回儿[groupPosition] .length;
        }

        公共查看getChildView(INT groupPosition,诠释childPosition,布尔isLastChild,
                查看convertView,ViewGroup中父){

            如果(convertView == NULL){
                LayoutInflater infalInflater =(LayoutInflater)上下文
                .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());
            返回convertView;
        }

        公共对象getGroup(INT groupPosition){
            返回组[groupPosition]
        }

        公众诠释getGroupCount(){
                返回groups.length;
        }

        众长getGroupId(INT groupPosition){
            返回groupPosition;
        }

        公共查看getGroupView(INT groupPosition,布尔isExpanded,查看convertView,
                ViewGroup中父){
            如果(convertView == NULL){
                LayoutInflater infalInflater =(LayoutInflater)上下文
                .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());
            返回convertView;
        }

        公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
            返回true;
        }

        公共布尔hasStableIds(){
            返回true;
        }
    }
}
 

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

这篇关于与扩展列表适配器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 10:23