gpt4 book ai didi

android - 如何将图像按钮添加到 Android 上的可扩展 ListView 的子项?

转载 作者:行者123 更新时间:2023-11-30 04:27:09 30 4
gpt4 key购买 nike

我有这段代码来创建 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 的 child 身上添加图像按钮。如何解决它并使其可点击?谢谢

最佳答案

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

ImageButton add;

并在onCreate方法中

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

findViewById 是在 setContentView 指定的布局里面寻找 View 。但这还没有发生。所以会发生错误。

关于android - 如何将图像按钮添加到 Android 上的可扩展 ListView 的子项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8291978/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com