ExpandableListActivity使用findViewById查找child中view的时序问题
2012-05-23 11:09
483 查看
在做一个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);
<a href="http://www.eye%3Ca%20href%3D/" http:="" www.eyeandroid.com"="" target="_blank" class="relatedlink" style="word-wrap: break-word; color: rgb(51, 102, 153); border-bottom-width: 1px; border-bottom-style:
solid; border-bottom-color: blue; ">Android.com/misc.php?mod=tag&id=182" target="_blank" class="relatedlink">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;
}
});
--------------------------------------------
}
在网上找到这样一个例子:
public class Act1 extends ExpandableListActivity { //需要从ExpandableListActivity继承
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //
Adapter1 ada=new Adapter1(); //Adapter1的定义下面,自定义视图是由它实现的
setListAdapter(ada);
}
public class Adapter1 extends BaseExpandableListAdapter {
private String[] groups = //初始化一些数据用于显示分组的标题,这个例子不是为了说明数据如何存取,所以这里用固定数据,使例子更突出重点。
{
"g1",
"g2",
"g3",
};
private String[][] children = //初始化一些数据用于显示每个分组下的数据项,这个例子不是为了说明数据如何存取,所以这里用固定数据,使例子更突出重点。
{
{ "name1" },
{ "name21", "name21" },
{ "name31", "name32", "name33" },
};
@Override
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition]; //获取数据,这里不重要,为了让例子完整,还是写上吧
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition; //
}
@Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length; //
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
//重点在这里
LayoutInflater inflate=LayoutInflater.from(Act1.this);
View view=inflate.inflate(R.layout.childlayout, null); //用childlayout这个layout作为条目的视图
ImageView contactIcon=(ImageView)view.findViewById(R.id.contactIcon); //childlayout有一个图标,
contactIcon.setImageResource(R.drawable.h001); //指定它的图片内容,就是示例图中的企鹅了
TextView name=(TextView)view.findViewById(R.id.name); //childlayout有一个用于显示名字的视图
name.setText(children[groupPosition][childPosition]); //给这个视图数据
TextView description=(TextView)view.findViewById(R.id.description); //childlayout有一个用于显示描述的视图,在name视图的下面,
description.setTextKeepState("description"); //这里只是简单的把它的数据设为description
ImageView mycursor=(ImageView)view.findViewById(R.id.myCursor);//childlayout还有一个小图标,在右侧,你可以给它一个单击事件,以弹出对当前条目的菜单。
return view;
}
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//父列表中的某一项的View
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
//这里的处理方法和getChildView()里的类似,不再重复说了
LayoutInflater inflate=LayoutInflater.from(Act1.this);
View view=inflate.inflate(R.layout.grouplayout, null); //用grouplayout这个layout作为条目的视图
TextView groupName=(TextView)view.findViewById(R.id.groupName);
String group="test group";
groupName.setText(group);
TextView groupCount=(TextView)view.findViewById(R.id.groupCount);
groupCount.setText("["+children[groupPosition].length+"]");
return view;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
在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);
<a href="http://www.eye%3Ca%20href%3D/" http:="" www.eyeandroid.com"="" target="_blank" class="relatedlink" style="word-wrap: break-word; color: rgb(51, 102, 153); border-bottom-width: 1px; border-bottom-style:
solid; border-bottom-color: blue; ">Android.com/misc.php?mod=tag&id=182" target="_blank" class="relatedlink">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;
}
});
--------------------------------------------
}
在网上找到这样一个例子:
public class Act1 extends ExpandableListActivity { //需要从ExpandableListActivity继承
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //
Adapter1 ada=new Adapter1(); //Adapter1的定义下面,自定义视图是由它实现的
setListAdapter(ada);
}
public class Adapter1 extends BaseExpandableListAdapter {
private String[] groups = //初始化一些数据用于显示分组的标题,这个例子不是为了说明数据如何存取,所以这里用固定数据,使例子更突出重点。
{
"g1",
"g2",
"g3",
};
private String[][] children = //初始化一些数据用于显示每个分组下的数据项,这个例子不是为了说明数据如何存取,所以这里用固定数据,使例子更突出重点。
{
{ "name1" },
{ "name21", "name21" },
{ "name31", "name32", "name33" },
};
@Override
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition]; //获取数据,这里不重要,为了让例子完整,还是写上吧
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition; //
}
@Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length; //
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
//重点在这里
LayoutInflater inflate=LayoutInflater.from(Act1.this);
View view=inflate.inflate(R.layout.childlayout, null); //用childlayout这个layout作为条目的视图
ImageView contactIcon=(ImageView)view.findViewById(R.id.contactIcon); //childlayout有一个图标,
contactIcon.setImageResource(R.drawable.h001); //指定它的图片内容,就是示例图中的企鹅了
TextView name=(TextView)view.findViewById(R.id.name); //childlayout有一个用于显示名字的视图
name.setText(children[groupPosition][childPosition]); //给这个视图数据
TextView description=(TextView)view.findViewById(R.id.description); //childlayout有一个用于显示描述的视图,在name视图的下面,
description.setTextKeepState("description"); //这里只是简单的把它的数据设为description
ImageView mycursor=(ImageView)view.findViewById(R.id.myCursor);//childlayout还有一个小图标,在右侧,你可以给它一个单击事件,以弹出对当前条目的菜单。
return view;
}
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//父列表中的某一项的View
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
//这里的处理方法和getChildView()里的类似,不再重复说了
LayoutInflater inflate=LayoutInflater.from(Act1.this);
View view=inflate.inflate(R.layout.grouplayout, null); //用grouplayout这个layout作为条目的视图
TextView groupName=(TextView)view.findViewById(R.id.groupName);
String group="test group";
groupName.setText(group);
TextView groupCount=(TextView)view.findViewById(R.id.groupCount);
groupCount.setText("["+children[groupPosition].length+"]");
return view;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
相关文章推荐
- [Android] ExpandableListActivity使用findViewById查找child中view的时序问题
- 使用findViewById查找自定义view时返回NULL问题
- 使用findViewById查找自定义view时返回NULL问题
- Android解决使用findViewById时需要对返回值进行类型转换问题的辅助类
- Android解决使用findViewById时需要对返回值进行类型转换问题的辅助类
- opengl的surfaceview使用findViewById返回null问题解决
- 注释参数opengl的surfaceview使用findViewById返回null问题解决
- TabActivity,ActivityGroup,ExpandableListActivity,ListView联合使用问题解决。
- Android解决使用findViewById时需要对返回值进行类型转换问题的辅助类
- 关于android Activity中注解的使用,省去无用的findviewbyid....setonclick.....
- Android解决使用findViewById时需要对返回值进行类型转换问题的辅助类
- 关于在adapter使用LinearLayout的findviewbyid问题
- find和mv的结合使用解决 arg list too long的问题
- 【总结】在两个Activity之间进行跳转时出错原因分析(一)——findViewById位置不当导致
- Android:认识R类、findViewById方法查找组件、@string查找字符、@color查找颜色、@drawable查找图片、@dimen某个组件尺寸定义、项目引入资源国际化
- 直接通过findViewById获取NavigationView中控件引用为null的问题
- 嵌套Fragment的使用及遇到The specified child already has a parent. You must call removeView()问题的解决
- androidの自定义控件View在Activity中使用findByViewId得到结果为null,解决方法。。
- android startActivtyForResult以及ExpandableListActivity的使用
- Android application - findViewById返回NULL的问题