0

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.

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68

4 Answers4

0

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" />
Alex Franco
  • 96
  • 1
  • 4
  • I forgot to say that I use Android Studio for programming. I left all default except these strings: public void cambiatesto......(Color.WHITE). The RelativeLayout is closed in my file, I think I didn't selecte it while coping. I use the FrameLayout only for convenience, because the drag and drop is easier... – Lorenzo Alinari May 23 '16 at 20:27
  • I don't understand how can I replace the FAB icon with mine. I have right-clicked res, new, image asset, i set it like and "image" (and not like a clipart or text), I sayed to do it circled, finish. But when I replace android:src="@drawable/my_icon" with "@drawable/zampa1.png" (the name I assigned to the icon) It don't found it in any resorce (Error:(26, 26) No resource found that matches the given name (at 'src' with value '@drawable/zampa1.png'). Thank you for your time :) – Lorenzo Alinari May 26 '16 at 19:06
0

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

k3np4ch1
  • 21
  • 1
  • Your explanation is very accurate in terms of why the exception was thrown. It would have been more helpful, in this case, to also include an example (`code`) of how to implement the `bau1` method - this is necessary since even the link you shared points to the general documentation for `RadioButton` and not an example. – ishmaelMakitla May 23 '16 at 18:11
  • Thank you, but how can I do this? I forgot to say that I'm using Android Studio for programming, and I am very new, I know a very few things... – Lorenzo Alinari May 23 '16 at 20:34
  • Lorenzo, in your case, I think its preferable to just remove the android:onClick="bau1". So by what I understand from the code, you are trying to do the following: When the button is cicked, you see which radio button is checked and accordingly set the text, text color and the background color for the TextView baubau. So if you remove the onClick in the XML file, i think it should work fine. – k3np4ch1 May 24 '16 at 05:44
  • @ishmaelMakitla thanks for the tip, I'm new to SttackOverflow, so will keep that in mind the next time I answer questions. :) – k3np4ch1 May 24 '16 at 05:54
0

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.

Community
  • 1
  • 1
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
  • I forgot to say that I use Android Studio for programming. I left all default except these strings: public void cambiatesto......(Color.WHITE). I set the onClick by using the "graphical tab". Why do I have to remove the onClick property? By doing this I can't assign an action. I am very new to Android programming and i know a very few things... – Lorenzo Alinari May 23 '16 at 20:33
  • So that I suggested using OnCheckedChangeListener linked with documentation as well as example knowing that you are a beginner.. Why don't you consider that? – Shree Krishna May 24 '16 at 01:08
0

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.

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • Thank you! It helped a lot! :). But how can I make the same thing with the CheckBoxes? I think that I have to change "RadioGroup group..." with something else, but what? Thanks again for your time :) – Lorenzo Alinari May 26 '16 at 18:19
  • If it helped and worked, please vote and accept the answer. The please post a separate question about CheckBoxes - we will help you there as well. – ishmaelMakitla May 27 '16 at 06:51
  • HI @LorenzoAlinari, did you post the question about `CheckBox`? You can put the link here and I can follow it and assist. Thanks again for indicating that the suggested answer solved your problem - please accept the answer so that your question can be marked as answered. – ishmaelMakitla May 27 '16 at 11:48