如何在 Android 上将图像按钮添加到 Expandable ListView 的子级?

发布于 2024-12-18 03:03:10 字数 3283 浏览 3 评论 0原文

我有这段代码来创建 Expandable ListView :

public class Menu extends ExpandableListActivity { 
    int l;
    ExpandableListAdapter mAdapter;
    ImageButton add = (ImageButton) findViewById(R.id.imageAdd);

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.week_menu);
        final String NAME = "NAME";
        final String EVENT = "EVENT";
        l = getIntent().getExtras().getInt("nilai");

        List<Map<String, String>> createGroupList = new ArrayList<Map<String, String>>();
        List<List<Map<String, String>>> createChildList = new ArrayList<List<Map<String, String>>>();
        for (int i = 0, k = 0; i < 5; i++) {
            Map<String, String> curGroupMap = new HashMap<String, String>();
            createGroupList.add(curGroupMap);
            if (i == 0)
                curGroupMap.put(NAME, "Breakfast 07.00");
            else if (i == 1)
                curGroupMap.put(NAME, "Snack 10.00");
            else if (i == 2)
                curGroupMap.put(NAME, "Lunch 12.00");
            else if (i == 3)
                curGroupMap.put(NAME, "Snack 16.00");
            else if (i == 4)
                curGroupMap.put(NAME, "Dinner 19.00");

            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            if (i == 1 || i == 3)
            {
                for (int j = 0; j < 6; j++, k++) {
                    if(AdapterDB.optimal_chromosomes.get(l).gens[k].id != 164) {
                        Map<String, String> curChildMap = new HashMap<String, String>();
                        children.add(curChildMap);
                        curChildMap.put(NAME, AdapterDB.optimal_chromosomes.get(l).gens[k].name);
                        curChildMap.put(EVENT, "" + add);
                    }
                }
            }
            else 
            {
                for (int j = 0; j < 12; j++, k++) {
                    if(AdapterDB.optimal_chromosomes.get(l).gens[k].id != 164) {
                        Map<String, String> curChildMap = new HashMap<String, String>();
                        children.add(curChildMap);
                        curChildMap.put(NAME, AdapterDB.optimal_chromosomes.get(l).gens[k].name);
                        curChildMap.put(EVENT, "" + add);
                    }
                }
            }
           /*
            add.setOnClickListener(new OnClickListener() {
                 public void onClick(View v) {

                 }
            });*/
            createChildList.add(children);
        }

        mAdapter = new SimpleExpandableListAdapter(this, createGroupList, android.R.layout.simple_expandable_list_item_1, new String [] {NAME}, new int[]{android.R.id.text1},  createChildList, android.R.layout.simple_expandable_list_item_2, new String[] {NAME, EVENT}, new int [] {android.R.id.text2});
        setListAdapter(mAdapter); 


    }

这是错误,java.lang.NullPointerException at

ImageButton add = (ImageButton) findViewById(R.id.imageAdd);

... 知道吗?我想在 Android 中的孩子上添加图像按钮。如何解决并使其可点击?谢谢

I have this code to create Expandable ListView :

public class Menu extends ExpandableListActivity { 
    int l;
    ExpandableListAdapter mAdapter;
    ImageButton add = (ImageButton) findViewById(R.id.imageAdd);

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.week_menu);
        final String NAME = "NAME";
        final String EVENT = "EVENT";
        l = getIntent().getExtras().getInt("nilai");

        List<Map<String, String>> createGroupList = new ArrayList<Map<String, String>>();
        List<List<Map<String, String>>> createChildList = new ArrayList<List<Map<String, String>>>();
        for (int i = 0, k = 0; i < 5; i++) {
            Map<String, String> curGroupMap = new HashMap<String, String>();
            createGroupList.add(curGroupMap);
            if (i == 0)
                curGroupMap.put(NAME, "Breakfast 07.00");
            else if (i == 1)
                curGroupMap.put(NAME, "Snack 10.00");
            else if (i == 2)
                curGroupMap.put(NAME, "Lunch 12.00");
            else if (i == 3)
                curGroupMap.put(NAME, "Snack 16.00");
            else if (i == 4)
                curGroupMap.put(NAME, "Dinner 19.00");

            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            if (i == 1 || i == 3)
            {
                for (int j = 0; j < 6; j++, k++) {
                    if(AdapterDB.optimal_chromosomes.get(l).gens[k].id != 164) {
                        Map<String, String> curChildMap = new HashMap<String, String>();
                        children.add(curChildMap);
                        curChildMap.put(NAME, AdapterDB.optimal_chromosomes.get(l).gens[k].name);
                        curChildMap.put(EVENT, "" + add);
                    }
                }
            }
            else 
            {
                for (int j = 0; j < 12; j++, k++) {
                    if(AdapterDB.optimal_chromosomes.get(l).gens[k].id != 164) {
                        Map<String, String> curChildMap = new HashMap<String, String>();
                        children.add(curChildMap);
                        curChildMap.put(NAME, AdapterDB.optimal_chromosomes.get(l).gens[k].name);
                        curChildMap.put(EVENT, "" + add);
                    }
                }
            }
           /*
            add.setOnClickListener(new OnClickListener() {
                 public void onClick(View v) {

                 }
            });*/
            createChildList.add(children);
        }

        mAdapter = new SimpleExpandableListAdapter(this, createGroupList, android.R.layout.simple_expandable_list_item_1, new String [] {NAME}, new int[]{android.R.id.text1},  createChildList, android.R.layout.simple_expandable_list_item_2, new String[] {NAME, EVENT}, new int [] {android.R.id.text2});
        setListAdapter(mAdapter); 


    }

It is error, java.lang.NullPointerException at

ImageButton add = (ImageButton) findViewById(R.id.imageAdd);

... any idea? I want to add imagebutton on the child in Android. How to solve it and make it clickable? Thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤千羽 2024-12-25 03:03:10

您需要在 setContentView 执行后在 onCreate 按钮中初始化该变量。

ImageButton add;

在 onCreate 方法中,

setContentView(R.layout.week_menu);
add = (ImageButton) findViewById(R.id.imageAdd);

findViewById 正在查找 setContentView 中指定的布局内的视图。但这还没有发生。所以会发生错误。

You need to initialize the variable in the onCreate button after setContentView executes.

ImageButton add;

And in the onCreate method

setContentView(R.layout.week_menu);
add = (ImageButton) findViewById(R.id.imageAdd);

findViewById is looking for the view inside the layout specified in setContentView. But that has not happened yet. So error will happen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文