可扩展列表适配器的问题

2024-01-01

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

我正在尝试创建一个可扩展的列表,我尝试过谷歌搜索,并阅读了谷歌文档。但不知怎的,我无法理解它。我的代码在 eclipse 中没有给出错误。但是当在模拟器中运行它时,它会在启动时崩溃。我认为当我设置时我做错了什么adapter to the view.

这是我的代码:

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 

我想到了。对于任何感兴趣的人,您所要做的就是将“Context”声明放入“My ExpandableListAdapter”中,并将“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;
        }
    }
} 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

可扩展列表适配器的问题 的相关文章

随机推荐

  • 自定义视图/操作/控制器在 Sails JS 中不起作用

    我一定做错了什么 但我看不到 基本上如果我通过创建一个控制器 模型sails generate controller products并在文件中 controllers ProductsController我向索引操作添加一些变量 例如 i
  • Laravel Facebook 登录 - 无回调(本地主机)

    我的第一个问题 是否可以在本地主机上进行测试 因为我可以想象这可能是我的问题 我阅读了文档并按照描述执行了所有操作 我浏览到 auth facebook 然后被重定向到 Facebook 在那里我 接受 这些条款 我被重定向到此链接 htt
  • Spring分页-请求参数

    我的 REST 控制器 GetMapping test public Page
  • nrwl/nx 工作区特定原理图

    我一直在研究 nrwl 扩展 它们看起来很棒 但是 当我按照他们的教程进行操作时工作区特定原理图 https nrwl io nx workspace specific schematics 最后一步没有显示要运行的命令 你能告诉我如何运行
  • Android - 如何检测屏幕上的触摸是“滚动”触摸?

    我正在用 Java 创建一个 Android 应用程序 其中有很多
  • 使用 Django/Twilio 进行条件短信响应

    我正在尝试根据不同的参数 来电显示 文本正文 调整短信响应 错误是 HTTP 检索失败 我尝试对不同的调用者使用 Flask 教程 def hello monkey Respond and greet the caller by name
  • 如何在浏览器上永久保存某些值?

    我有一些登录信息 假设用户名 登录电子邮件和位置 即使用户注销并关闭窗口后 我也希望将此信息保留在浏览器中 当用户注销或会话过期后返回时 Web 应用程序会填写客户端用户名并要求用户提供密码 我的要求最好的例子是谷歌登录 目前 我只使用会话
  • SharePoint 2010 中的沙盒解决方案和场解决方案有什么区别

    我是 SharePoint 2010 的新手 沙箱和场解决方案之间的主要区别是什么 你能给我 5 个要记住的差异吗 我用谷歌搜索了 但有很多事情需要理解 作为一个刚起步的人 我想知道基本的区别 感谢您的帮助 农场解决方案 服务器场解决方案托
  • Unity - Firebase 实时数据库 - 获取我在排行榜中的排名

    我有一个使用 firebase 数据库实时进行排行榜的迷你游戏 从 firebase 获取用户分数列表后 我想获取不在列表中的当前用户的分数 获取当前用户的分数很容易 但是如何知道列表中的排名 即 OrderByChild score 这是
  • 从套接字 fd 获取 IP 地址、端口和连接类型

    我有一个跟踪网络应用程序的跟踪程序 给定一个属于被跟踪者的套接字文件描述符 跟踪器是否有可能找到它对应的IP 对于服务器来说是它绑定到的接口的IP 对于客户端来说是被绑定接口的地址 用于建立连接的接口 端口号和连接类型 是否有 libc A
  • 如何从 Linux 获取窗口大小

    每个人 我对编程还是新手 我确实需要一些帮助来解决我面临的问题 所以 这里的情况是我试图在终端尺寸低于 80x24 时显示警告 根据记录 我的操作系统是 Window 但我使用虚拟机来运行 Linux 因为所有文件都在 Linux 中 当我
  • Kibana 不搜索嵌套字段

    使用 Elasticsearch Kibana 并尝试在嵌套对象中的字段中进行搜索 然而它似乎不起作用 这是我在模板中使用的映射 order 0 template ss7 signaling settings index mapping t
  • IE 10 相对位置元素在父元素中滚动后消失

    元素与位置 相对在特定情况下 位于大表格中的表格单元格内的 会在 Windows 7 上的 Internet Explorer 10 中消失 向下滚动页面 滚动div scroller向右 滚动到顶部 所有浏览器均按预期工作 IE10 显示
  • 如何在 Android 中捕获图像并将其显示在图库中?

    之前我捕捉到的图像出现在画廊中 但现在它们没有出现 我不明白为什么 这是我的代码 ContentValues values new ContentValues values put android provider MediaStore I
  • 在 JavaScript 中,如何在不修改原始对象的情况下修改嵌套对象的值?

    如何在不使用额外空间的情况下将所有嵌套对象值更改为 true 任何人都可以帮助我吗 我已经尝试过这种方式 但我没有得到处理嵌套对象的逻辑 P S 请不要关注字符串中的 true 或 false 因为它是模拟数据 我只想实现我未能做到的逻辑
  • 防止锁传播

    在 bash 下进行锁定的一种简单且看似可靠的方法是 exec 9 gt gt lockfile flock 9 然而 众所周知 bash 会将这样的 fd 锁传播到所有分叉的东西 包括执行的程序等 有什么办法告诉 bash 不要重复 fd
  • 如何将服务暴露到k8s集群之外?

    我已经使用以下命令运行了 Hello World 应用程序 kubectl run hello world replicas 2 labels run load balancer example image gcr io google sa
  • 重新安装 WAMP,未找到 WordPress 表,但在 PHPMYADMIN 中

    好吧 情况很奇怪 我希望我不仅仅是运气不好 我从 Windows 7 升级到 8 当我升级时 WAMP 无法工作 我不假思索地重新安装了 WAMP 并按照网上的说明让 WAMP 正常工作 现在 我的 WordPress 站点将我重定向到安装
  • 两次dispatchKeyEvent调用方法

    我在我的活动中实现了dispatchKeyEvent来监听按下的Enter键 问题是 当我单击 Enter 时 它会调用我的方法两次 我怎样才能解决这个问题 谢谢 祝你有美好的一天 Override public boolean dispa
  • 可扩展列表适配器的问题

    我是Android开发的新手 所以我希望有人能帮助我解决这个问题 我正在尝试创建一个可扩展的列表 我尝试过谷歌搜索 并阅读了谷歌文档 但不知怎的 我无法理解它 我的代码在 eclipse 中没有给出错误 但是当在模拟器中运行它时 它会在启动