在做一个Demo时遇到了这样一个问题:

在ExpandableListActivity的onCreate方法中去查找子节点中的view会出现空指针异常错误,原因可能是时序不对,还不明。

public class ExpandableList extends ExpandableListActivity {
/** Called when the activity is first created. */
private final String TAG = "Expand";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.getExpandableListView().setBackgroundDrawable(getResources().getDrawable(R.drawable.default_bg));


ImageView imageView = (ImageView)findViewById(R.id.imageview);
TextView textView = (TextView)findViewById(R.id.textview);
Button button = (Button)findViewById(R.id.button);
if(button == null)
{
Log.v(TAG, "Can't find the button");
}
button.setOnTouchListener(new OnTouchListener() {


@Override
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG, "onTouch");
if(v.getId() == R.id.button)
{

Button button = (Button)v;
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
button.setBackgroundResource(R.drawable.edit_over);
}
else if(event.getAction() == MotionEvent.ACTION_UP)
{
button.setBackgroundResource(R.drawable.edit);
}
}
return false;
}
});

--------------------------------------------

}


在网上找到这样一个例子:

  1. publicclassAct1extendsExpandableListActivity{//需要从ExpandableListActivity继承
  2. @Override
  3. publicvoidonCreate(BundlesavedInstanceState){
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.main);//
  6. Adapter1ada=newAdapter1();//Adapter1的定义下面,自定义视图是由它实现的
  7. setListAdapter(ada);
  8. }
  9. publicclassAdapter1extendsBaseExpandableListAdapter{
  10. privateString[]groups=//初始化一些数据用于显示分组的标题,这个例子不是为了说明数据如何存取,所以这里用固定数据,使例子更突出重点。
  11. {
  12. "g1",
  13. "g2",
  14. "g3",
  15. };
  16. privateString[][]children=//初始化一些数据用于显示每个分组下的数据项,这个例子不是为了说明数据如何存取,所以这里用固定数据,使例子更突出重点。
  17. {
  18. {"name1"},
  19. {"name21","name21"},
  20. {"name31","name32","name33"},
  21. };
  22. @Override
  23. publicObjectgetChild(intgroupPosition,intchildPosition){
  24. returnchildren[groupPosition][childPosition];//获取数据,这里不重要,为了让例子完整,还是写上吧
  25. }
  26. @Override
  27. publiclonggetChildId(intgroupPosition,intchildPosition){
  28. returnchildPosition;//
  29. }
  30. @Override
  31. publicintgetChildrenCount(intgroupPosition){
  32. returnchildren[groupPosition].length;//
  33. }
  34. @Override
  35. publicViewgetChildView(intgroupPosition,intchildPosition,booleanisLastChild,ViewconvertView,ViewGroupparent){
  36. //重点在这里
  37. LayoutInflaterinflate=LayoutInflater.from(Act1.this);
  38. Viewview=inflate.inflate(R.layout.childlayout,null);//用childlayout这个layout作为条目的视图
  39. ImageViewcontactIcon=(ImageView)view.findViewById(R.id.contactIcon);//childlayout有一个图标,
  40. contactIcon.setImageResource(R.drawable.h001);//指定它的图片内容,就是示例图中的企鹅了
  41. TextViewname=(TextView)view.findViewById(R.id.name);//childlayout有一个用于显示名字的视图
  42. name.setText(children[groupPosition][childPosition]);//给这个视图数据
  43. TextViewdescription=(TextView)view.findViewById(R.id.description);//childlayout有一个用于显示描述的视图,在name视图的下面,
  44. description.setTextKeepState("description");//这里只是简单的把它的数据设为description
  45. ImageViewmycursor=(ImageView)view.findViewById(R.id.myCursor);//childlayout还有一个小图标,在右侧,你可以给它一个单击事件,以弹出对当前条目的菜单。
  46. returnview;
  47. }
  48. @Override
  49. publicObjectgetGroup(intgroupPosition){
  50. returngroups[groupPosition];
  51. }
  52. @Override
  53. publicintgetGroupCount(){
  54. returngroups.length;
  55. }
  56. @Override
  57. publiclonggetGroupId(intgroupPosition){
  58. returngroupPosition;
  59. }
  60. //父列表中的某一项的View
  61. @Override
  62. publicViewgetGroupView(intgroupPosition,booleanisExpanded,ViewconvertView,ViewGroupparent){
  63. //这里的处理方法和getChildView()里的类似,不再重复说了
  64. LayoutInflaterinflate=LayoutInflater.from(Act1.this);
  65. Viewview=inflate.inflate(R.layout.grouplayout,null);//用grouplayout这个layout作为条目的视图
  66. TextViewgroupName=(TextView)view.findViewById(R.id.groupName);
  67. Stringgroup="testgroup";
  68. groupName.setText(group);
  69. TextViewgroupCount=(TextView)view.findViewById(R.id.groupCount);
  70. groupCount.setText("["+children[groupPosition].length+"]");
  71. returnview;
  72. }
  73. @Override
  74. publicbooleanhasStableIds(){
  75. returntrue;
  76. }
  77. @Override
  78. publicbooleanisChildSelectable(intgroupPosition,intchildPosition){
  79. returntrue;
  80. }
  81. }
  82. }

更多相关文章

  1. 一个侧屏滑动操作的实例(仿遇见)之三:代码分析
  2. android存取数据(基础)
  3. Android(安卓)Studio快速添加Gson以及GsonFormat的使用
  4. Android(安卓)IQ包 发送 基于XMPP
  5. Android(安卓)ApiDemos示例解析(9):App->Activity->Persistent St
  6. android 5.0新特性学习--视图阴影
  7. android 中 intent 重点
  8. Android(安卓)ListView Adapter的getItemViewType和getViewTypeC
  9. android 解析json数据(一)

随机推荐

  1. android系统架构图及各层介绍
  2. Android线程处理简述
  3. 【Android每日一讲】2012.11.08 Android
  4. 在荷兰Android占有70%的智能手机市场份额
  5. Handler内存泄露原理及解决方法
  6. Android运行环境Dalvik和ART的比较
  7. Android源码下载之《Android新闻客户端源
  8. [置顶] 杂谈——Android从启动到程序运行
  9. Android 和 iPhone 界面布局示例
  10. LeanCanary——消除Android中的内存泄露