0

I have a TextView which contains onclick in xml.The onclick works sometimes and sometimes the app crashes.I renamed the TextView in xml to android.support.v7.widget.AppCompatTextView,but it didn't solved the issue.I found similar questions but none of solved mine.

code

 public void customerDetails(View view){
    Intent intent=new Intent(Customers.this,CustomerDetails.class);
    intent.putExtra(Common.CUSTOMERID,view.getTag().toString());
    startActivity(intent);
}

My XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        app:cardCornerRadius="4dp"
        app:cardElevation="1dp"
        app:cardMaxElevation="0dp"
        app:cardUseCompatPadding="true"
        android:animateLayoutChanges="true">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">

        <TextView
            android:id="@+id/customer_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@+id/call_button"
            android:layout_toLeftOf="@+id/amount"
            android:layout_toRightOf="@+id/call_button"
            android:layout_toStartOf="@+id/amount"
            android:onClick="customerDetails"
            android:text="Customer Name"
            android:textColor="@color/colorAccent"
            android:textSize="14sp"
            android:textStyle="bold" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/customer_name"
            android:text="ContactPersonName"
            android:textColor="@color/primary_text"
            android:textSize="12sp"
            android:id="@+id/contact_person_name"
            android:layout_toLeftOf="@+id/amount"
            android:layout_toStartOf="@+id/amount"
            android:layout_toRightOf="@+id/call_button"
            android:layout_toEndOf="@+id/call_button"/>

    </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

Error is

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

Anand
  • 1,866
  • 3
  • 26
  • 49

3 Answers3

0

You have to use your own class in xml, as standrt TextView classes do not contain your custom method

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
0

Try Adding this line to the root tag of the xml:

tools:context="<yourpackaggename.activityname>"
0

You should add your listeners in your Activity onCreate method. By using lambdas, it won't grow your code more than a line for every listener:

findViewById(R.id.textView).setOnClickListener(v -> customerDetails(v));

And you can change customerDetails method visibility to private now.

This is a better practice, and you will have a better control on your listeners if you need anything more complex.

DAA
  • 1,346
  • 2
  • 11
  • 19