我正在使用可擴展列表視圖。因爲我需要使用可點擊的圖像瀏覽。我試過以下但不工作。它給出空指針異常。在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。
任何想法??
對於ListView控件,你必須用一個'Adapter'(ListView顯示的數據)餵它,從你的代碼中我看不到一個。如果您提到的imageview是listview項目,您可以簡單地重寫ListView的onItemClick(),您不需要爲每個項目設置「OnClickListener」。 – neevek 2012-03-10 08:14:05
在這裏發佈你的'main.xml'佈局。 – 2012-03-10 08:24:54