Miguel Zarate
Miguel Zarate

Reputation: 13

java.lang.IllegalStateException: Could not execute method for android:onClick

There is the XML layout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lab02pc19.imhere.FragmentBluetooth">

    <!-- TODO: Update blank fragment layout -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal|top"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView2"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="44dp"
            android:layout_marginStart="44dp"
            android:layout_marginTop="70dp"
            android:background="@drawable/ic_action_bluetooth_connected"
            android:clickable="true"
            android:onClick="miImagen" />
    </RelativeLayout>
</FrameLayout>

I am working on an app that works with Bluetooth and I need that the app turns on and turn off the cell phone Bluetooth on the click of ImageView.

This is the mistake

05-25 07:43:41.129 18314-18314/com.example.lab02pc19.imhere E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.lab02pc19.imhere, PID: 18314
                                                                              java.lang.IllegalStateException: Could not find method clickImagen(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageView with id 'imablue'
                                                                                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:325)
                                                                                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                                  at android.view.View.performClick(View.java:5201)
                                                                                  at android.view.View$PerformClick.run(View.java:21163)
                                                                                  at android.os.Handler.handleCallback(Handler.java:746)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

There is my java code

package com.example.lab02pc19.imhere;

import android.bluetooth.BluetoothAdapter;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, FragmentOpciones.OnFragmentInteractionListener,FragmentBluetooth.OnFragmentInteractionListener {
   ImageView miimagen;
    BluetoothAdapter bluetoothAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        miimagen = (ImageView) findViewById(R.id.imablue);
        View view = new View(this);


        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        if(bluetoothAdapter==null){
            miimagen.setVisibility(View.GONE);
        }
        else
        {
            setEstadoBluetooth(bluetoothAdapter.isEnabled());

        }


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    private void view(MainActivity mainActivity) {
    }

    private void setEstadoBluetooth(boolean enabled) {
    }

    private void setImageBluetooth(boolean enabled) {
    }
    public void clickima(View view){
        switch (view.getId()){
            case R.id.imablue:
                setEstadoBluetooth();
                break;

        }
    }

    public void setImagenBluetooth(Boolean valor){
        if(valor) miimagen.setImageResource(R.drawable.ic_action_bluetooth_connected);
        else miimagen.setImageResource(R.drawable.ic_action_bluetooth);
    }
    public void setEstadoBluetooth(){
        if(bluetoothAdapter.isEnabled()){
            setImagenBluetooth(false);
            bluetoothAdapter.disable();

        }
        else{
            setImagenBluetooth(true);
            bluetoothAdapter.enable();
        }
    }



    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();


        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {

        int id = item.getItemId();
        boolean FragmentTransaction = false;
                Fragment fragment = null;

        if (id == R.id.nav_opciones){
         fragment = new FragmentOpciones();
            FragmentTransaction = true;

        }else if(id== R.id.nav_Bluetooth){
            fragment = new FragmentBluetooth();
            FragmentTransaction = true;



        }
        if(FragmentTransaction){
            getSupportFragmentManager().beginTransaction().replace(R.id.content_main, fragment).commit();
            item.setChecked(true);
            getSupportActionBar().setTitle(item.getTitle());
        }


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}

Upvotes: 1

Views: 960

Answers (1)

Gaurav Singh
Gaurav Singh

Reputation: 2320

In your xml file inside ImageView you have written android:onClick="miImagen" it means on clicking that imageview a method named as miImagen will be called .

But you haven't written any miImagen() method in your java code. So you have to create a method named as miImagen and perform desired fuctionality inside that method.

Moreover in your java code you have written:

miimagen = (ImageView) findViewById(R.id.imablue);

But in your xml there is no such imageView having id as imablue. I think in above line of code instead imablue it should be imageView2 which is the ID of your imageView in your xml file.

Upvotes: 1

Related Questions