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:
(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.