ExpandableListActivity使用findViewById查找child中view的时序问题

来源:互联网 发布:美团大数据 编辑:程序博客网 时间:2024/05/19 03:27

在做一个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;
}
    });

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

}


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

  1. public class Act1 extends ExpandableListActivity { //需要从ExpandableListActivity继承  
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.           
  6.         setContentView(R.layout.main);  //  
  7.           
  8.         Adapter1 ada=new Adapter1(); //Adapter1的定义下面,自定义视图是由它实现的  
  9.         setListAdapter(ada);   
  10.     }  
  11.   
  12.     public class Adapter1 extends BaseExpandableListAdapter {   
  13.       
  14.         private String[] groups = //初始化一些数据用于显示分组的标题,这个例子不是为了说明数据如何存取,所以这里用固定数据,使例子更突出重点。  
  15.         {  
  16.             "g1",  
  17.             "g2",  
  18.             "g3",  
  19.         };  
  20.           
  21.         private String[][] children = //初始化一些数据用于显示每个分组下的数据项,这个例子不是为了说明数据如何存取,所以这里用固定数据,使例子更突出重点。  
  22.         {  
  23.             { "name1" },  
  24.             { "name21""name21" },  
  25.             { "name31""name32""name33" },  
  26.         };  
  27.           
  28.         @Override  
  29.         public Object getChild(int groupPosition, int childPosition) {  
  30.             return children[groupPosition][childPosition];  //获取数据,这里不重要,为了让例子完整,还是写上吧  
  31.         }  
  32.       
  33.         @Override  
  34.         public long getChildId(int groupPosition, int childPosition) {  
  35.             return childPosition; //  
  36.         }  
  37.       
  38.         @Override  
  39.         public int getChildrenCount(int groupPosition) {  
  40.             return children[groupPosition].length; //  
  41.         }  
  42.       
  43.         @Override  
  44.         public View getChildView(int groupPosition, int childPosition,  boolean isLastChild, View convertView, ViewGroup parent) {  
  45.             //重点在这里  
  46.             LayoutInflater inflate=LayoutInflater.from(Act1.this);  
  47.             View view=inflate.inflate(R.layout.childlayout, null); //用childlayout这个layout作为条目的视图  
  48.               
  49.             ImageView contactIcon=(ImageView)view.findViewById(R.id.contactIcon); //childlayout有一个图标,  
  50.             contactIcon.setImageResource(R.drawable.h001);  //指定它的图片内容,就是示例图中的企鹅了  
  51.               
  52.             TextView name=(TextView)view.findViewById(R.id.name); //childlayout有一个用于显示名字的视图  
  53.             name.setText(children[groupPosition][childPosition]); //给这个视图数据  
  54.               
  55.             TextView description=(TextView)view.findViewById(R.id.description); //childlayout有一个用于显示描述的视图,在name视图的下面,  
  56.             description.setTextKeepState("description");  //这里只是简单的把它的数据设为description  
  57.               
  58.             ImageView mycursor=(ImageView)view.findViewById(R.id.myCursor);//childlayout还有一个小图标,在右侧,你可以给它一个单击事件,以弹出对当前条目的菜单。  
  59.               
  60.             return view;  
  61.         }  
  62.       
  63.         @Override  
  64.         public Object getGroup(int groupPosition) {  
  65.             return groups[groupPosition];  
  66.         }  
  67.       
  68.         @Override  
  69.         public int getGroupCount() {  
  70.             return groups.length;  
  71.         }  
  72.       
  73.         @Override  
  74.         public long getGroupId(int groupPosition) {  
  75.             return groupPosition;  
  76.         }  
  77.       
  78.         //父列表中的某一项的View  
  79.         @Override  
  80.         public View getGroupView(int groupPosition, boolean isExpanded,  View convertView, ViewGroup parent) {  
  81.             //这里的处理方法和getChildView()里的类似,不再重复说了  
  82.               
  83.             LayoutInflater inflate=LayoutInflater.from(Act1.this);  
  84.             View view=inflate.inflate(R.layout.grouplayout, null);  //用grouplayout这个layout作为条目的视图  
  85.               
  86.             TextView groupName=(TextView)view.findViewById(R.id.groupName);  
  87.             String group="test group";  
  88.             groupName.setText(group);  
  89.               
  90.             TextView groupCount=(TextView)view.findViewById(R.id.groupCount);  
  91.             groupCount.setText("["+children[groupPosition].length+"]");  
  92.               
  93.             return view;  
  94.         }  
  95.       
  96.         @Override  
  97.         public boolean hasStableIds() {  
  98.             return true;  
  99.         }  
  100.       
  101.         @Override  
  102.         public boolean isChildSelectable(int groupPosition, int childPosition) {  
  103.             return true;  
  104.         }  
  105.           
  106.     }  
  107. }