java.lang.IllegalStateException:无法执行android:onClick的方法

时间:2016-05-25 13:53:40

标签: android bluetooth

有XML布局

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

我正在开发一款适用于蓝牙的应用程序,我需要通过点击ImageView打开并关闭手机蓝牙。

这是错误

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)

有我的java代码

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

    }
}

1 个答案:

答案 0 :(得分:1)

ImageView 中的xml文件中,您已经编写了android:onClick="miImagen",这意味着在点击该imageview时,一个名为 miImagen 将被调用。

但是你没有在java代码中编写任何 miImagen()方法。因此,您必须创建一个名为 miImagen 的方法,并在该方法中执行所需的功能。

此外,在您的java代码中,您已经写过:

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

但是在你的xml中没有这样的imageView,其id为 imablue 。我认为在上面的代码行中, imablue 应该是 imageView2 ,这是xml文件中imageView的ID。