2017-09-13 3 views
-1

Ich habe drei textviews in Activity und zu erkennen, welche textview gedrückt wird, verwende ich unten Methode und Einstellung spinner Wert entsprechend, aber Aktivität erkennt diese Methode nicht. Ich habe viele Möglichkeiten ausprobiert, aber nicht den Fehler bekommen. wird abgestürzt braucht Hilfe, wenn ich auf textview Anwendung klicken undMethode onClick_leave (View) in einem Eltern- oder Vorfahren

java.lang.IllegalStateException zeigt diesen Fehler protokollieren: Konnte nicht Methode onClick_leave (View) in einem Elternteil oder einem Vorfahren Kontext für Android finden: onClick definiert Attribut auf Ansichtsklasse android.support.v7.widget.AppCompatTextView mit id 'pl'

Hier ist mein 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-Datei:

<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

Mögliches Duplikat [Könnte ein Verfahren onClick (Ansicht) in der Aktivität nicht gefunden] (https://stackoverflow.com/questions/9786058/could-not-find-a-method-onclickview- in-the-activity) – sushildlh

+0

haben Sie 'View.OnClickListener' beim Erstellen Ihrer Klasse implementiert? – sam

+0

posten Sie Ihre komplette XML-Datei, da diese Textansichten in Linear Layout für mich funktioniert – Anonymous

Antwort

-1

sollten Sie setOnClickListener verwenden.

Registrieren Sie einen Rückruf, der aufgerufen werden soll, wenn auf diese Ansicht geklickt wird. Wenn diese Ansicht nicht anklickbar ist, wird sie anklickbar.

 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 
     } 
    } 
+0

Wie Schalter gelöst OP-Probleme? – sushildlh

Verwandte Themen