简体   繁体   中英

java.lang.IllegalStateException: Could not find method

I think that is a dumb error, and I am new to programming. I was making an app to learn: there are 2 radio buttons to choose what text show in an hidden TextView, 2 check box to choose the background and text color of the TextView and a button, that do a check of the check box and the radio buttons and print the text in the TextView. But when in the emulator I try to click something, the app crashes, and the logcat says that it can't find the id and the onClick. Why do I get this?

The program:

package com.bauapp.lory.bauapp;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;
import android.graphics.Color;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton)
    findViewById(R.id.fab);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action",Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

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

    return super.onOptionsItemSelected(item);
}
public void cambiaTesto (View v) {



    TextView baubau = (TextView) findViewById(R.id.bauText);
    RadioButton bau1 = (RadioButton) findViewById(R.id.bauRadio1);
    RadioButton bau2 = (RadioButton) findViewById(R.id.bauRadio2);
    CheckBox testoRosso = (CheckBox)findViewById(R.id.c1);
    CheckBox sfondoGiallo = (CheckBox)findViewById(R.id.c2);

    if(bau1.isChecked()) {
        baubau.setText("BAU");
    }
    else
    if (bau2.isChecked()) {
        baubau.setText("BAUUUUUUU");
    }

    if(testoRosso.isChecked())
        baubau.setTextColor(Color.RED);

    else
        baubau.setTextColor(Color.BLACK);
    if (sfondoGiallo.isChecked())
        baubau.setBackgroundColor(Color.YELLOW);

    else
        baubau.setBackgroundColor(Color.WHITE);


    }
}

The XML:

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 app:layout_behavior="@string/appbar_scrolling_view_behavior"
 tools:context="com.bauapp.lory.bauapp.MainActivity"
 tools:showIn="@layout/activity_main">

 <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="43dp">

    <TextView
        android:layout_width="342dp"
        android:layout_height="191dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/bauText"
        android:layout_gravity="center_horizontal|bottom"
        android:onClick="baubau" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Testo rosso"
        android:id="@+id/c1"
        android:layout_gravity="left|center_vertical"
        android:onClick="testoRosso"
        android:checked="false"
        android:clickable="true" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sfondo giallo"
        android:id="@+id/c2"
        android:layout_gravity="right|center_vertical"
        android:onClick="sfondoGiallo"
        android:clickable="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BAU BAU?"
        android:id="@+id/bauButton"
        android:layout_gravity="right|top"
        android:onClick="cambiaTesto" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|top">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BAU"
            android:id="@+id/bauRadio1"
            android:layout_gravity="left|top"
            android:onClick="bau1"
            android:checked="false"
            android:clickable="true" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BAUUUUUUU"
            android:id="@+id/bauRadio2"
            android:layout_gravity="left|top"
            android:onClick="bau2"
            android:checked="false"
            android:clickable="true" />

    </RadioGroup>

</FrameLayout>

And the logcat:

http://textuploader.com/5blrn

(I leave an external link because it doesn't paste correctly for a bug in stackoverflow).

The crash logcat is the same for all the other buttons, but, obviously, change the ID and the OnClick. Sorry for my bad English.

Ciao Lorenzo,

I noticed at the end of your XML file: you forgot to close the RelativeLayout with the proper closetag </RelativeLayout> . It's only a copy-paste mystake or your xml file is incorrect?

I give you a tip. Avoid filling the xml file with too many layout when with a few changes you can get the same view with fewer elements. In your example the FrameLayout is completely useless.

EDIT: In your layout i didn't see the <FloatingActionButton> . Make sure to add the support library dependence in your build.grandle file:

  • Remember to change the version based on your api target * compile 'com.android.support:design:23.2.1'

After that you can add the FAB in you layout like this:

<android.support.design.widget.FloatingActionButton
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="bottom|right"
              android:layout_margin="16dp"
              android:src="@drawable/my_icon" />

The Radio Button needs a method to handle its onClick event. You have given the onClick method as android:onClick="bau1" for android:id="@+id/bauRadio1", but the MainActivity class needs to implement the bau1 method. Hence the IllegalStateException of cannot find method bau1. Check this link for more details on RadioButton and Responding to Click Events: https://developer.android.com/guide/topics/ui/controls/radiobutton.html

Either create

public void bau1(View view){
   //Perform some code
}

In your MainActivity

OR Remove android:onClick="bau1" from XML

Specially I prefer using OnCheckedChangeListener instead of onClick in case of RadioGroup like shown here .

My suggestion would be to remove android:onClick=... from your RadioButtons, and instead use onCheckedChangeListener on the RadioGroup and then determine which RadioButton was checked and then do whatever you need to do. Your code changes are then as follows:

<RadioGroup
        android:id="@+id/bauRadioGroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|top">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BAU"
            android:id="@+id/bauRadio1"
            android:layout_gravity="left|top"
            android:checked="false"
            android:clickable="true" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BAUUUUUUU"
            android:id="@+id/bauRadio2"
            android:layout_gravity="left|top"
            android:checked="false"
            android:clickable="true" />
    </RadioGroup>

Then in your MainActivity onCreate you add this code:

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.bauRadioGroup);        
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId == R.id.bauRadio1){
            //radio button bauRadio1 was clicked
              baubau.setText("BAU");
            }
           else if(checkedId == R.id.bauRadio2 ){
             //radio button bauRadio2 was clicked
             baubau.setText("BAUUUUUUU");
           }
        }
    });
  //The rest of your code ...

Please give this a try and let us know if this helps. As already suggested, How to set On click listener on the Radio Button in Android is a good example for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM