MY layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="3"
>
<ImageView
android:id="@+id/photo_user"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/user_pic"
android:layout_gravity="top"
android:contentDescription="@string/image_descrip"
/>
<TextView
android:id="@+id/text_user"
android:layout_alignRight="@+id/photo_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user_info"
android:textColor="#FFFFFF"
android:background="#AA000000"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="7"
android:background="#000000"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="15dp"
android:text="@string/plan_text0"
/>
<TextView
android:id="@+id/tv_viewplan"
android:layout_width="wrap_content"
android:paddingLeft="100dp"
android:textSize="12sp"
android:layout_height="wrap_content"
android:text="@string/plan_text"
/>
<TextView
android:id="@+id/tv_plan1"
android:paddingLeft="215dp"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/plan_text1"
/>
</RelativeLayout>
<View
android:id="@+id/place_view1"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginTop="40dp"
android:background="#5C5C5C"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/place_view1"
android:paddingTop="10dp"
android:paddingBottom="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="15dp"
android:text="@string/services_text0"
/>
<TextView
android:id="@+id/tv_service"
android:layout_width="wrap_content"
android:paddingLeft="100dp"
android:textSize="12sp"
android:layout_height="wrap_content"
android:text="@string/services_text"
/>
<TextView
android:id="@+id/tv_service1"
android:layout_below="@+id/tv_plan1"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="215dp"
android:text="@string/services_text1"
/>
</RelativeLayout>
<View
android:id="@+id/place_view2"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginTop="80dp"
android:background="#5C5C5C"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/place_view2"
android:paddingTop="10dp"
android:paddingBottom="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="15dp"
android:text="@string/bill_text0"
/>
<TextView
android:id="@+id/tv_bill"
android:layout_width="wrap_content"
android:paddingLeft="100dp"
android:textSize="12sp"
android:layout_height="wrap_content"
android:text="@string/bill_text"
/>
<TextView
android:id="@+id/tv_bill1"
android:layout_below="@+id/tv_service1"
android:paddingLeft="215dp"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bill_text1"
/>
</RelativeLayout>
<View
android:id="@+id/place_view3"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginTop="120dp"
android:background="#5C5C5C"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/place_view3"
android:paddingTop="10dp"
android:paddingBottom="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="15dp"
android:text="@string/request_text0"
/>
<TextView
android:id="@+id/tv_request"
android:layout_width="wrap_content"
android:paddingLeft="100dp"
android:textSize="12sp"
android:layout_height="wrap_content"
android:text="@string/request_text"
/>
<TextView
android:id="@+id/tv_request1"
android:paddingLeft="215dp"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/request_text1"
/>
</RelativeLayout>
<View
android:id="@+id/place_view4"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginTop="160dp"
/>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/place_view4"
>
<ExpandableListView
android:id="@+id/ex_notifi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></ExpandableListView>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
在我的夏洛克片段类中,我对所有文本视图都有相同的侦听器
getView().findViewById(R.id.tv_viewplan).setOnClickListener(this);
getView().findViewById(R.id.tv_plan1).setOnClickListener(this);
getView().findViewById(R.id.tv_service).setOnClickListener(this);
getView().findViewById(R.id.tv_service1).setOnClickListener(this);
getView().findViewById(R.id.tv_bill).setOnClickListener(this);
getView().findViewById(R.id.tv_bill1).setOnClickListener(this);
getView().findViewById(R.id.tv_request).setOnClickListener(this);
getView().findViewById(R.id.tv_request1).setOnClickListener(this);
在我的点击侦听器中
switch(v.getId())
{
case R.id.tv_viewplan:
System.out.println("Plan ids" +v.getId());
title= "My Plan"; tag = "viewplan_dialog";
Utilsdialog dialog_vwplan = new Utilsdialog(context, R.layout.child, title, tag);
dialog_vwplan.show(fm, tag);
break;
case R.id.tv_plan1:
System.out.println("Plan ids" +v.getId());
title= "Change plan"; tag = "changeplan_dialog";
Utilsdialog dialog_chgplan = new Utilsdialog(context, R.layout.child,title, tag);
dialog_chgplan.show(fm, tag);
break;
case R.id.tv_service:
Toast.makeText(getActivity(), "SELECTED VIEW SERVICE", Toast.LENGTH_LONG).show();
break;
case R.id.tv_service1:
Toast.makeText(getActivity(), "SELECTED ADD SERVICE", Toast.LENGTH_LONG).show();
break;
case R.id.tv_request:
Toast.makeText(getActivity(), "SELECTED TRACK REQUEST", Toast.LENGTH_LONG).show();
break;
case R.id.tv_request1:
Toast.makeText(getActivity(), "SELECTED ADD REQUEST", Toast.LENGTH_LONG).show();
break;
case R.id.tv_bill:
Toast.makeText(getActivity(), "SELECTED VIEW BILL", Toast.LENGTH_LONG).show();
break;
case R.id.tv_bill1:
Toast.makeText(getActivity(), "SELECTED PAY BILL", Toast.LENGTH_LONG).show();
break;
}
问题在于tv_viewplan和tv_plan生成相同的 id,以及具有相同 ID 的tv_service和tv_servive1。 所以切换调用不正确的块。 我不知道这怎么可能...请帮助
不同 ID 名称的不同视图在一次不能具有相同的 ID。从来没有,永远。检查每个视图都有不同的 ID 名称。每个视图都有另一种检查标记的方法,您应该为每个视图分配唯一的标记。但从逻辑上讲,第一个应该工作。
您需要清理项目。
如果您使用的是 eclipse,请在菜单中打开您的项目选项,请在项目的子菜单中找到清洁选项。
选择您的项目,然后单击弹出窗口中的确定按钮。
如果你这样做,那么你可以为你的项目获取新的id。
如果您在清理项目后遇到任何问题,请告诉我。
清理并重新生成项目,并确保每个文本视图上没有重复的 ID
如果不起作用,请尝试执行此操作
你为什么不全部删除 getView().findViewById(R.id.tv_viewplan).setOnClickListener(this);
并添加 android:onclick="clickText"
在每个文本中可单击的视图
然后添加此函数
public void clickText(View v){
//put your switch case here...
}