Could not find method onClick_leave(View) in a parent or ancestor

–1 vote
asked Sep 13, 2017 by axay

i have three textviews in Activity and to Recognise which textview is pressed i am using below method and setting spinner's value accordingly but activity is not recognising this method. I have tried many ways but not getting the mistake. need Help when i click on textview Application gets crashed and log shows this error

java.lang.IllegalStateException: Could not find method onClick_leave(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatTextView with id 'pl'

Here is my code:

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 file :

 <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>

1 Answer

–1 vote
answered Mar 9, 2018 by intellij-amiya

You should use setOnClickListener.

Register a callback to be invoked when this view is clicked. If this view is not clickable, it becomes clickable.

 firstButtonOBJ = (Button) findViewById(R.id.buttonOne); //Global firstButtonOBJ.setOnClickListener(this); secondButtonOBJ = (Button) findViewById(R.id.buttonTwo); secondButtonOBJ.setOnClickListener(this); // onClick is called when a view has been clicked. @Override public void onClick(View v) { // Parameter v stands for the view that was clicked. // getId() returns this view's identifier. if(v.getId() == R.id.buttonOne) { // Your Work } else if(v.getId() == R.id.buttonTwo) { // Your Work } }
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
...