我有三個textviews
in Activity
並確認哪個textview
被按下,我正在使用下面的方法並相應地設置了微調器的值,但活動並未識別此方法。 我嘗試了很多方法,但沒有收到錯誤。需要幫忙 當我textview
應用點擊獲取墜毀,日誌顯示此錯誤在父代或祖先中找不到onClick_leave(View)的方法
java.lang.IllegalStateException:在 Android的父母或祖先上下文找不到方法 onClick_leave(查看):onclick屬性定義在視圖類 android.support.v7.widget.AppCompatTextView ID爲 'PL'
這裏是我的代碼:
public void onClick_leave(View v) {
if (v.getId() == R.id.cl) {
Intent intent = new Intent(getActivity(), ApplyLeave.class);
int leave = 0;
intent.putExtra("leave", leave);
startActivity(intent);
} else if (v.getId() == R.id.pl) {
Intent intent = new Intent(getActivity(), ApplyLeave.class);
int leave = 1;
intent.putExtra("leave", leave);
startActivity(intent);
} else if (v.getId() == R.id.sl) {
Intent intent = new Intent(getActivity(), ApplyLeave.class);
int leave = 2;
intent.putExtra("leave", leave);
startActivity(intent);
} else if (v.getId() == R.id.edit_menu) {
Intent intent = new Intent(getActivity(), ApplyLeave.class);
startActivity(intent);
}
}
xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
xmlns:design="http://schemas.android.com/apk/res-auto"
tools:context="com.example.axay.o2hleave.Main2Activity">
<LinearLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:layout_marginTop="14dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/o2hOne"
android:orientation="vertical"
android:clipToPadding="false"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
android:layout_weight="0.2">
<TextView
android:id="@+id/cl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/o2hOne"
android:gravity="center"
android:onClick="onClick_leave"
android:padding="5dp"
android:text="7"
android:textColor="@color/o2htextOne"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Casual Leave"
android:elevation="15dp"
android:textSize="15sp"
android:padding="5dp"
android:layout_gravity="center"
android:textColor="@color/o2htextOne"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/o2hforth"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:elevation="6dp">
<TextView
android:id="@+id/pl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/o2hforth"
android:elevation="6dp"
android:gravity="center"
android:onClick="onClick_leave"
android:padding="5dp"
android:text="21"
android:textColor="@color/o2htextOne"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Personal Leave"
android:elevation="5dp"
android:textSize="15sp"
android:layout_gravity="center"
android:textColor="@color/o2htextOne"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/o2hfifth"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="1dp"
android:elevation="6dp"
android:layout_weight="0.60">
<TextView
android:id="@+id/sl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/o2hfifth"
android:elevation="6dp"
android:gravity="center"
android:onClick="onClick_leave"
android:padding="5dp"
android:text=""
android:textColor="@color/o2htextOne"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sick Leave"
android:textSize="15sp"
android:padding="5dp"
android:gravity="center"
android:layout_gravity="center"
android:textColor="@color/o2htextOne"/>
</LinearLayout>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/frame_layout"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textStyle="bold"
android:padding="7dp"
android:textSize="16sp"
android:text=" Leave Requests"
/>
<TextView
android:id="@+id/no_leave_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textStyle="bold"
android:textSize="20sp"
android:padding="40dp"
android:visibility="gone"
android:drawableRight="@drawable/sad"
android:text="You Have no Leave Requests"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_LeaveRequests"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp">
</android.support.v7.widget.RecyclerView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Approval Pending Leaves"
android:padding="7dp"
android:textAlignment="center"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/no_pending_leaves"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textStyle="bold"
android:textSize="20sp"
android:padding="40dp"
android:visibility="gone"
android:drawableRight="@drawable/sad"
android:text="You Have no Pending leaves"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_PendingLeaves"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
>
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
的[找不到在活動的方法的onClick(視圖)](可能的複製https://stackoverflow.com/questions/9786058/could-not-find-a-method-onclickview-在活動中) – sushildlh
您是否在創建類時實現了'View.OnClickListener'? – sam
發佈完整的xml文件,因爲包含在Linear Layout中的文本視圖正在爲我工作 – Anonymous