[Android] ExpandableListActivity使用findViewById查找child中view的时序问题
在做一个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;
}
});
--------------------------------------------
}
在网上找到这样一个例子:
- publicclassAct1extendsExpandableListActivity{//需要从ExpandableListActivity继承
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);//
- Adapter1ada=newAdapter1();//Adapter1的定义下面,自定义视图是由它实现的
- setListAdapter(ada);
- }
- publicclassAdapter1extendsBaseExpandableListAdapter{
- privateString[]groups=//初始化一些数据用于显示分组的标题,这个例子不是为了说明数据如何存取,所以这里用固定数据,使例子更突出重点。
- {
- "g1",
- "g2",
- "g3",
- };
- privateString[][]children=//初始化一些数据用于显示每个分组下的数据项,这个例子不是为了说明数据如何存取,所以这里用固定数据,使例子更突出重点。
- {
- {"name1"},
- {"name21","name21"},
- {"name31","name32","name33"},
- };
- @Override
- publicObjectgetChild(intgroupPosition,intchildPosition){
- returnchildren[groupPosition][childPosition];//获取数据,这里不重要,为了让例子完整,还是写上吧
- }
- @Override
- publiclonggetChildId(intgroupPosition,intchildPosition){
- returnchildPosition;//
- }
- @Override
- publicintgetChildrenCount(intgroupPosition){
- returnchildren[groupPosition].length;//
- }
- @Override
- publicViewgetChildView(intgroupPosition,intchildPosition,booleanisLastChild,ViewconvertView,ViewGroupparent){
- //重点在这里
- LayoutInflaterinflate=LayoutInflater.from(Act1.this);
- Viewview=inflate.inflate(R.layout.childlayout,null);//用childlayout这个layout作为条目的视图
- ImageViewcontactIcon=(ImageView)view.findViewById(R.id.contactIcon);//childlayout有一个图标,
- contactIcon.setImageResource(R.drawable.h001);//指定它的图片内容,就是示例图中的企鹅了
- TextViewname=(TextView)view.findViewById(R.id.name);//childlayout有一个用于显示名字的视图
- name.setText(children[groupPosition][childPosition]);//给这个视图数据
- TextViewdescription=(TextView)view.findViewById(R.id.description);//childlayout有一个用于显示描述的视图,在name视图的下面,
- description.setTextKeepState("description");//这里只是简单的把它的数据设为description
- ImageViewmycursor=(ImageView)view.findViewById(R.id.myCursor);//childlayout还有一个小图标,在右侧,你可以给它一个单击事件,以弹出对当前条目的菜单。
- returnview;
- }
- @Override
- publicObjectgetGroup(intgroupPosition){
- returngroups[groupPosition];
- }
- @Override
- publicintgetGroupCount(){
- returngroups.length;
- }
- @Override
- publiclonggetGroupId(intgroupPosition){
- returngroupPosition;
- }
- //父列表中的某一项的View
- @Override
- publicViewgetGroupView(intgroupPosition,booleanisExpanded,ViewconvertView,ViewGroupparent){
- //这里的处理方法和getChildView()里的类似,不再重复说了
- LayoutInflaterinflate=LayoutInflater.from(Act1.this);
- Viewview=inflate.inflate(R.layout.grouplayout,null);//用grouplayout这个layout作为条目的视图
- TextViewgroupName=(TextView)view.findViewById(R.id.groupName);
- Stringgroup="testgroup";
- groupName.setText(group);
- TextViewgroupCount=(TextView)view.findViewById(R.id.groupCount);
- groupCount.setText("["+children[groupPosition].length+"]");
- returnview;
- }
- @Override
- publicbooleanhasStableIds(){
- returntrue;
- }
- @Override
- publicbooleanisChildSelectable(intgroupPosition,intchildPosition){
- returntrue;
- }
- }
- }
更多相关文章
- 一个侧屏滑动操作的实例(仿遇见)之三:代码分析
- android存取数据(基础)
- Android(安卓)Studio快速添加Gson以及GsonFormat的使用
- Android(安卓)IQ包 发送 基于XMPP
- Android(安卓)ApiDemos示例解析(9):App->Activity->Persistent St
- android 5.0新特性学习--视图阴影
- android 中 intent 重点
- Android(安卓)ListView Adapter的getItemViewType和getViewTypeC
- android 解析json数据(一)