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