[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;
}
    });

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

}


在網上找到這樣一個例子:

  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. }  

發佈了50 篇原創文章 · 獲贊 10 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章