簡體   English   中英

如何在Android的Expandable ListView的子項中添加imagebutton?

[英]How to Add imagebutton to child of Expandable ListView on Android?

我有以下代碼來創建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在

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

... 任何想法? 我想在Android中的孩子上添加imagebutton。 如何解決並使其可點擊? 謝謝

setContentView執行后,需要在onCreate按鈕中初始化變量。

ImageButton add;

並在onCreate方法中

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

findViewById在setContentView中指定的布局內查找視圖。 但這還沒有發生。 所以會發生錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM