Button definition in OnCreate - Cannot resolve symbol in method
I'll start as you will see. I don't see where I can legally put Button statements and setText statements for this to compile. If I translate one, the other doesn't work and vice versa. I understand that OnCreate is protected and therefore "buttonOne" will not be passed to the playPhrases method, but I've been changing things for a while now and nothing seems to work. Simple explanations.
package com.example.android.languageapp;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import static android.R.attr.button;
public class MainActivity extends AppCompatActivity {
MediaPlayer myMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myMediaPlayer = MediaPlayer.create(this, R.raw.hello);
String[] phrases = getResources().getStringArray(R.array.phrase);
Button buttonOne = (Button) findViewById(R.id.button1);
Button buttonTwo = (Button) findViewById(R.id.button2);
Button buttonThree = (Button) findViewById(R.id.button3);
Button buttonFour = (Button) findViewById(R.id.button4);
Button buttonFive = (Button) findViewById(R.id.button5);
Button buttonSix = (Button) findViewById(R.id.button6);
Button buttonSeven = (Button) findViewById(R.id.button7);
Button buttonEight = (Button) findViewById(R.id.button8);
buttonOne.setText(phrases[0]);
buttonTwo.setText(phrases[1]);
buttonThree.setText(phrases[2]);
buttonFour.setText(phrases[3]);
buttonFive.setText(phrases[4]);
buttonSix.setText(phrases[5]);
buttonSeven.setText(phrases[6]);
buttonEight.setText(phrases[7]);
}
public void playPhrases(View clickedButton) {
int id = clickedButton.getId();
Log.i("Button id", "" + id);
if (id == buttonOne.getId()) {
myMediaPlayer = MediaPlayer.create(this, R.raw.doyouspeakenglish);
myMediaPlayer.start();
}
}
}
thanks for the help
source to share
Declare your buttons outside of the onCreate method and then add them to views in onCreate
. This way you can access them later anywhere in your class. I showed this using 3 buttons as an example, but you can do it the same for everyone.
These buttons become class data members or global variables, not local variables of the onCreate method. To learn more about Global Vairables visit Here
public class MainActivity extends AppCompatActivity {
MediaPlayer myMediaPlayer;
Button buttonOne ,buttonTwo,buttonThree ; // Do it this way first for buttons to create reference
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myMediaPlayer = MediaPlayer.create(this, R.raw.hello);
String[] phrases = getResources().getStringArray(R.array.phrase);
// Access like this
buttonOne = (Button) findViewById(R.id.button1);
buttonTwo = (Button) findViewById(R.id.button2);
buttonThree = (Button) findViewById(R.id.button3);
Button buttonFour = (Button) findViewById(R.id.button4);
Button buttonFive = (Button) findViewById(R.id.button5);
Button buttonSix = (Button) findViewById(R.id.button6);
Button buttonSeven = (Button) findViewById(R.id.button7);
Button buttonEight = (Button) findViewById(R.id.button8);
buttonOne.setText(phrases[0]);
buttonTwo.setText(phrases[1]);
buttonThree.setText(phrases[2]);
buttonFour.setText(phrases[3]);
buttonFive.setText(phrases[4]);
buttonSix.setText(phrases[5]);
buttonSeven.setText(phrases[6]);
buttonEight.setText(phrases[7]);
}
public void playPhrases(View clickedButton) {
int id = clickedButton.getId();
Log.i("Button id", "" + id);
if (id == buttonOne.getId()) {
myMediaPlayer = MediaPlayer.create(this, R.raw.doyouspeakenglish);
myMediaPlayer.start();
}
}
}
source to share
define Button variables all over the world try this
public class MainActivity extends AppCompatActivity {
MediaPlayer myMediaPlayer;
Button buttonOne;
Button buttonTwo ;
Button buttonThree ;
Button buttonFour;
Button buttonFive;
Button buttonSix ;
Button buttonSeven ;
Button buttonEight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myMediaPlayer = MediaPlayer.create(this, R.raw.hello);
String[] phrases = getResources().getStringArray(R.array.phrase);
buttonOne = (Button) findViewById(R.id.button1);
buttonTwo = (Button) findViewById(R.id.button2);
buttonThree = (Button) findViewById(R.id.button3);
buttonFour = (Button) findViewById(R.id.button4);
buttonFive = (Button) findViewById(R.id.button5);
buttonSix = (Button) findViewById(R.id.button6);
buttonSeven = (Button) findViewById(R.id.button7);
buttonEight = (Button) findViewById(R.id.button8);
buttonOne.setText(phrases[0]);
buttonTwo.setText(phrases[1]);
buttonThree.setText(phrases[2]);
buttonFour.setText(phrases[3]);
buttonFive.setText(phrases[4]);
buttonSix.setText(phrases[5]);
buttonSeven.setText(phrases[6]);
buttonEight.setText(phrases[7]);
}
public void playPhrases(View clickedButton) {
int id = clickedButton.getId();
Log.i("Button id", "" + id);
if (id == buttonOne.getId()) {
myMediaPlayer = MediaPlayer.create(this, R.raw.doyouspeakenglish);
myMediaPlayer.start();
}
}
}
source to share
You only use id
in playPhrases
to listen for clicks like this,
public void playPhrases(View clickedButton) {
int id = clickedButton.getId();
Log.i("Button id", "" + id);
switch (id) {
case R.id.R.id.button1:
// Button 1 operation
myMediaPlayer = MediaPlayer.create(this, R.raw.doyouspeakenglish);
myMediaPlayer.start();
break;
case R.id.R.id.button2:
// Button 2 operation
break;
case R.id.R.id.button3:
// Button 3 operation
break;
}
// We dont need button object
//if (id == buttonOne.getId()) {
//
// myMediaPlayer = MediaPlayer.create(this, R.raw.doyouspeakenglish);
//
// myMediaPlayer.start();
//}
}
source to share
You must move your button variable declarations from the OnCreate method to declare it globally. ButtonOne button; Must be in the MainActivity class if you declare it in a method that it cannot use in another; Then you can still: buttonOne = (button) findViewById (R.id.button1); in OnCreate
source to share