0

When I am clicking on Image its not responding. But when i add onClick() in XML so it gets to crash. I am having multiple images to do same help me with it.

XML file

  <ImageView
            android:id="@+id/cto"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:onClick="onClick"
            android:layout_alignBottom="@+id/fb"
            android:layout_toLeftOf="@+id/fb"
            android:layout_toStartOf="@+id/fb"
            android:src="@drawable/callto" />

Java File

    public class Content_main extends AppCompatActivity {

    private ImageView call;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_main);

        call = findViewById(R.id.cto);

        call.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel: 9871275493"));
                if (ActivityCompat.checkSelfPermission(Content_main.this,
                        Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                startActivity(callIntent);
            }
        });

    }
}

Its Error when I add onClick value & if I am not adding it's not responding

03-29 17:24:57.188 4158-4158/com.flavourbasket.flavourbasket E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.flavourbasket.flavourbasket, PID: 4158
    java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageView with id 'cto'
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
        at android.view.View.performClick(View.java:6294)
        at android.view.View$PerformClick.run(View.java:24770)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
  • Your **`error`** says something else but your code says something else, !! **According to the error , you havent used any AppcompatImageView in your code** – Santanu Sur Mar 29 '18 at 12:17
  • @SantanuSur this is not true. onClick is a method of the View class (superclass of ImageView) and the reason is the wrong xml attribute... – appersiano Mar 29 '18 at 12:37
  • `on view class android.support.v7.widget.AppCompatImageView with id 'cto'` i still dint get this :- @appersiano can you explain ? He is using `imageView` not `AppCompatImageView` ..both are completely different widgets !! – Santanu Sur Mar 29 '18 at 12:40
  • @santanu Sir please, Stop this And tell me the Solution. Tomorrow is the deadLine. – Vivek Pathak Mar 29 '18 at 12:52
  • @SantanuSur a copy and paste mistake by the user I think – appersiano Mar 29 '18 at 12:59
  • @VivekPathak first run your project and paste the latest codes and latest errors !! – Santanu Sur Mar 29 '18 at 13:07

4 Answers4

1

Try this You just forgot @Override

  call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel: 9871275493"));
                if (ActivityCompat.checkSelfPermission(Content_main.this,
                        Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                startActivity(callIntent);
            }
        });

EDIT

also remove android:onClick="onClick" from your ImageView

<ImageView
    android:id="@+id/cto"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignBottom="@+id/fb"
    android:layout_toLeftOf="@+id/fb"
    android:layout_toStartOf="@+id/fb"
    android:src="@drawable/callto" />
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

Remove this line from your xml to solve the crash

 android:onClick="onClick"

next remember to, ask for runtime permission with requestPermission method (example)

ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.CALL_PHONE},
                MY_PERMISSIONS_REQUEST_CALL_PHONE);
appersiano
  • 2,670
  • 22
  • 42
0

Is either you add the onclick method from xml, or set the listener using

setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {}
        }
Kuti Gbolahan
  • 2,379
  • 1
  • 9
  • 17
0

I think it could be the casting. you are not casting imageview. try casting it

Aimkiller
  • 263
  • 1
  • 5
  • 13