2012-03-10 80 views
1

我正在使用可扩展列表视图。因为我需要使用可点击的图像浏览。我试过以下但不工作。它给出空指针异常。在imageview上设置clicklistner

这里是我的xml和listview适配器。我需要在imageview上放置点击事件。从正常的活动onclicklistner工作perfact,但如果你把它放在ExpandableListActivity给出错误。

在此先感谢..

public class ExpList extends ExpandableListActivity 
{ 
// static final String parent_node[] = { "grey", "blue", "yellow", "red" }; 

String parent_node[]; 

static final String shades[][] = { 
    { 
    "lightgrey","#D3D3D3", 
    "dimgray","#696969", 
    "sgi gray 92","#EAEAEA" 
    }, {}, 
    { 
    "dodgerblue 2","#1C86EE", 
    "steelblue 2","#5CACEE", 
    "powderblue","#B0E0E6" 
    }, {} 
}; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle icicle) 
{ 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 


    LayoutInflater group_inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE); 
    View group_layout = group_inflater.inflate(R.layout.group_row, null); 

    ImageView img_call = (ImageView)group_layout.findViewById(R.id.imgcall); 

    img_call.setOnClickListener(new OnClickListener() 
    {   
    @Override 
    public void onClick(View v) 
     { 
     Toast.makeText(getBaseContext(), "clicked...", Toast.LENGTH_SHORT).show(); 
    }); 

SimpleExpandableListAdapterWithEmptyGroups expListAdapter = 
      new SimpleExpandableListAdapterWithEmptyGroups(
       this, 
       createGroupList(),      
       R.layout.group_row,      
       new String[] { "colorName" },   
       new int[] { R.id.groupname },   
       createChildList(),      
       R.layout.child_row,      
       new String[] { "shadeName", "rgb" },  
       new int[] { R.id.childname, R.id.rgb } 
      ); 
     setListAdapter(expListAdapter);} 

而且我groupview XML是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

<ImageView 
    android:src="@drawable/expander_group" 
    android:id="@+id/explist_indicator" 
    android:layout_width="35px" 
    android:layout_height="35px"/> 


<ImageView 
    android:src="@drawable/contact_thumbnail" 
    android:id="@+id/img_contact_thumbnail" 
    android:layout_width="50px" 
    android:layout_height="50px"/> 

<TextView 
    android:id="@+id/groupname" 
    android:layout_width="190px" 
    android:layout_height="match_parent" 
    android:paddingLeft="30px" 
    android:textSize="16px" 
    android:textStyle="bold" /> 

<ImageView 
    android:id="@+id/imgcall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/call" 
    android:clickable="true" /> 
</LinearLayout> 

编辑
现在NullPointerException异常问题得到解决。但是我发现了onclicklistner无法正常工作的新问题。没有吐司或其他任何写在该部分不执行。我调试程序,发现当点击imageview时代码不会返回onclicklistner。

任何想法??

+0

对于ListView控件,你必须用一个'Adapter'(ListView显示的数据)喂它,从你的代码中我看不到一个。如果您提到的imageview是listview项目,您可以简单地重写ListView的onItemClick(),您不需要为每个项目设置“OnClickListener”。 – neevek 2012-03-10 08:14:05

+0

在这里发布你的'main.xml'布局。 – 2012-03-10 08:24:54

回答

1

你可以试试这个,希望它完全可以工作。只需添加两个线路在XML中的ImageView然后写一个函数监听按钮点击您的活动类:

<ImageView 
    android:id="@+id/imgcall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/call" 
    android:clickable="true" 
    android:onClick="onClickHandler"/> 

而在你的活动类:

public void onClickHandler (View v) { 
    switch (v.getId()) { 
     case R.id.imgcall: 
      // do what ever you want here ... 
      break; 
    } 
} 
+0

好的,谢谢..这是工作..但不能理解为什么内联clicklistner不工作??? – 2012-03-13 09:27:01