I am attempting to use some of Google's code from their audio capture sample code. They simplified the heck out of their code and made their layout within the class. I want to have an actual xml layout. I know how to do that part, but I would like to know how to change the code below to an onClick method and have all the functionality that is provided with it.
class PlayButton extends Button {
boolean mStartPlaying = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText("Stop playing");
} else {
setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
};
public PlayButton(Context ctx) {
super(ctx);
setText("Start playing");
setOnClickListener(clicker);
}
}
Any help is appreciated.
In the layout file, you'll have something like...
<LinearLayout>
<Button android:id="play_button"/>
</LinearLayout>
In the activity, onCreate(), you can then do something like..
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText("Stop playing");
} else {
setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
};
Button b = findViewById(R.id.play_button);
b.setOnClickListener(clicker);
ALTERNATELY, you can also define the method in the xml layout that will be called in the Activity...
<LinearLayout>
<Button android:id="play_button" onclick="play"/>
</LinearLayout>
and then in the Activity you simply create a method, called play(View view)
public void play(View view) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText("Stop playing");
} else {
setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
Just define the buttons as Button and declare the booleans as Activity variables. Example...
public class AudioRecordTest extends Activity {
...
private Button mPlayButton = null;
private boolean mStartPlaying = true;
// Do the same for mRecordButton and mStartRecording
...
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// The next line assumes the play button has the id "#+id/play_button"
mPlayButton = (Button)findViewById(R.id.play_button);
mPlayButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
((Button)v).setText("Stop playing");
} else {
((Button)v).setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
});
// Do the same for the mRecordButton
}
}
Extending button just to set an onClickListener is not a good idea. You should only extend something when you are going to add new functionality to it. Not when you are going to use it for a particular purpose that does not require additional functionality.
Button button = new Button(this);
button.setOnClickListener(...);
If you need to use XML, you can load it programmatically with a LayoutInflater.
Your boolean isPlaying is not a property of the button itself but of the media it is playing. You should not hide it inside the button.
Related
I am creating an app which uses a Toolbar like fragment at the top with a few buttons. The buttons are added programmatically.
The problem is that when I launch the app, the toolbar and buttons appear, but the buttons can't be clicked. When the buttons are moused over (I use an emulator), or clicked, they don't change at all, and don't notify my OnClickListener. However, the buttons and other components in the Fragment below it work perfectly.
The Toolbar's code:
public class ToolbarFragment extends Fragment implements
View.OnClickListener{
public static final String LOG_KEY = "SM_TOOLBAR";
public static final String TO_ADD_FEED_KEY = "TO_ADD_FEED_KEY";
public static final String TO_ADD_PROFILE_KEY = "TO_ADD_PROFILE_KEY";
public static final String TO_ADD_FRIENDS_KEY = "TO_ADD_FRIENDS_KEY";
private Button feed;
private Button profile;
private Button friends;
private Button logout;
public ToolbarFragment() {
// Required empty public constructor
}
private Button addButton(int stringID, LinearLayout linearLayout) {
Button button = new Button(getContext());
button.setText(stringID);
button.setOnClickListener(this);
linearLayout.addView(button);
return button;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
LinearLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.fragment_toolbar, container, false);
Bundle arguments = getArguments();
if (arguments.getBoolean(TO_ADD_FEED_KEY)) {
Log.i(LOG_KEY, "Created feed key");
feed = addButton(R.string.feed, linearLayout);
}
if (arguments.getBoolean(TO_ADD_PROFILE_KEY)) {
Log.i(LOG_KEY, "Created profile Key");
profile = addButton(R.string.profile, linearLayout);
}
if (arguments.getBoolean(TO_ADD_FRIENDS_KEY)) {
Log.i(LOG_KEY, "Created friends key");
friends = addButton(R.string.friends, linearLayout);
}
logout = addButton(R.string.logout, linearLayout);
return linearLayout;
}
#Override
public void onClick(View view) {
Log.i(LOG_KEY, "A button was clicked.");
if (getActivity() instanceof IToolbarCallback) {
IToolbarCallback itc = (IToolbarCallback) getActivity();
if (view.equals(feed)) {
itc.feed();
}
if (view.equals(profile)) {
itc.profile();
}
if (view.equals(friends)) {
itc.friends();
}
if (view.equals(logout)) {
itc.logout();
}
}
}
}
There's no other code pertinent to this, besides the callback
interface.
public interface IToolbarCallback {
void feed();
void profile();
void logout();
void friends();
}
This is just used to let the host activity know what was clicked.
Finally, the XML:
<LinearLayout 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="project3.csc214.project23.Fragments.Toolbar.ToolbarFragment"
android:orientation="horizontal">
I use a builder for the fragment, and here's the code:
public class ToolbarBuilder {
private boolean addFeed;
private boolean addProfile;
private boolean addFriends;
private FragmentManager fragmentManager;
private int addToID;
public ToolbarBuilder(FragmentManager fragmentManager, int addToID) {
this.fragmentManager = fragmentManager;
this.addToID = addToID;
addFeed = false;
addProfile = false;
addFriends = false;
}
public ToolbarBuilder addFeed() {
addFeed = true;
return this;
}
public ToolbarBuilder addProfile() {
addProfile = true;
return this;
}
public ToolbarBuilder addFriends() {
addFriends = true;
return this;
}
public void build() {
Bundle bundle = new Bundle();
bundle.putBoolean(ToolbarFragment.TO_ADD_FEED_KEY, addFeed);
bundle.putBoolean(ToolbarFragment.TO_ADD_FRIENDS_KEY, addFriends);
bundle.putBoolean(ToolbarFragment.TO_ADD_PROFILE_KEY, addProfile);
ToolbarFragment toolbarFragment = new ToolbarFragment();
toolbarFragment.setArguments(bundle);
fragmentManager.beginTransaction().add(addToID, toolbarFragment).commit();
}
}
Just to clarify, the buttons are receiving no inputs as far as I can tell. They aren't just failing to call onClick, they're failing to react in any way at all. As far as I know, onClick is set up correctly, the buttons are just broken on some fundamental level.
The plot has thickened. Using the exact same setups in other activities seems to make it work fine... As far as I can tell, there is were no changes.
Regardless, I decided just to hard code it for this activity so I could move on to other parts of the app. Thank you all for the consideration of the problem. I'll post again should I ever figure out what happened.
Don't compare views to see if their equal, compare ids. Here:
#Override
public void onClick(View view) {
Log.i(LOG_KEY, "A button was clicked.");
if (getActivity() instanceof IToolbarCallback) {
IToolbarCallback itc = (IToolbarCallback) getActivity();
if (view.equals(feed)) {
itc.feed();
}
if (view.equals(profile)) {
itc.profile();
}
if (view.equals(friends)) {
itc.friends();
}
if (view.equals(logout)) {
itc.logout();
}
}
}
It should be:
#Override
public void onClick(View view) {
Log.i(LOG_KEY, "A button was clicked.");
if (getActivity() instanceof IToolbarCallback) {
IToolbarCallback itc = (IToolbarCallback) getActivity();
if (view.getId() == feed.getId()) {
itc.feed();
}
if (view.getId() == profile.getId()) {
itc.profile();
}
if (view.getId() == friends.getId()) {
itc.friends();
}
if (view.getId() == logout.getId()) {
itc.logout();
}
}
}
Moreover, as you're creating the views yourself you need to also give them ids. If you're on API level 17+ you can simply call generateViewId() on the view and Android will create an unique id for you.
So do it like this:
private Button addButton(int stringID, LinearLayout linearLayout) {
Button button = new Button(getContext());
button.setText(stringID);
button.setOnClickListener(this);
linearLayout.addView(button);
button.generateViewId();
return button;
}
EDIT:
Your code seem fine, apart from what I've specified above. One thing I would try is setting the listener outside your addButton method:
private Button addButton(int stringID, LinearLayout linearLayout) {
Button button = new Button(getContext());
button.setText(stringID);
linearLayout.addView(button);
button.generateViewId();
return button;
}
if (arguments.getBoolean(TO_ADD_FEED_KEY)) {
Log.i(LOG_KEY, "Created feed key");
feed = addButton(R.string.feed, linearLayout);
feed.setOnClickListener(this);
}
if (arguments.getBoolean(TO_ADD_PROFILE_KEY)) {
Log.i(LOG_KEY, "Created profile Key");
profile = addButton(R.string.profile, linearLayout);
profile.setOnClickListener(this);
}
if (arguments.getBoolean(TO_ADD_FRIENDS_KEY)) {
Log.i(LOG_KEY, "Created friends key");
friends = addButton(R.string.friends, linearLayout);
friends.setOnClickListener(this);
}
logout = addButton(R.string.logout, linearLayout);
logout.setOnClickListener(this);
If that doesn't work, try to check if there isn't any other view on top of your buttons that's intercepting the click events.
This is probably because your Activity host haven't implementing the IToolbarCallback. You need to check for it with something like this:
#Override
public void onClick(View view) {
Log.i(LOG_KEY, "A button was clicked.");
if (getActivity() instanceof IToolbarCallback) {
Log.i(LOG_KEY, "IToolbarCallback is implemented.");
} else {
Log.e(LOG_KEY, "No IToolbarCallback implemented!!!");
}
}
The problem is you did not set any id to button. So in that case all buttons have same id. You have to set a unique id explicitly to each button .
private Button addButton(int stringID,int id, LinearLayout linearLayout) {
Button button = new Button(this);
button.setText(stringID);
button.setId(id);
button.setOnClickListener(this);
linearLayout.addView(button);
return button;
}
Then add a Id to each button . Keep that in mind that id should be unique and it must be a positive int. Or you can use view.generateViewId(); directly.
feed = addButton(R.string.feed,1, linearLayout);
profile = addButton(R.string.profile,2, linearLayout);
friends = addButton(R.string.friends,3, linearLayout);
logout = addButton(R.string.logout, 4,linearLayout);
Then modify your onClick() as follows
#Override
public void onClick(View v) {
switch (v.getId()) {
case feed.getId():
itc.feed();
break;
case profile.getId():
itc.profile();
break;
case friends.getId():
itc.friends();
break;
case logout.getId():
itc.logout();
break;
default:
break;
}
}
I am new to development field. I am developing android application where in my activity_main layout I have many different items. On clicking one of the button the top layout is replaced with a new layout.
Now instead of defining the new layouts buttons, textview etc in my main class , I want to have a separate class which can initialize my buttons etc, and also in that class I can declare onClickListners.
In my main Activity I want:-
public void onCreate(){
button bb = (Button)findViewById(R.id.mybutton);
View CurrentContentView= getLayoutInflater().inflate(R.layout.activity_main, null, false);
bb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyNewViewInit(CurrentContentView);
}
});
}
In my MyNewViewInit() class :-
public class MyNewViewInit{
ImageButton backbutton;
ChipsMultiAutoCompleteTextview search;
ImageButton searchclear;
ImageButton modeTime;
ImageButton modeTag;
TextView modeTimeTxt;
TextView modeTagTxt;
ScrollView mainScroll;
ScrollView selectedScroll;
public MyNewViewInit(View v){
backbutton = (ImageButton)v.findViewById(R.id.backbutton);
search = (ChipsMultiAutoCompleteTextview)v.findViewById(R.id.search);
searchclear = (ImageButton)v.findViewById(R.id.searchclear);
modeTime = (ImageButton)v.findViewById(R.id.modeTime);
modeTag = (ImageButton)v.findViewById(R.id.modeTag);
modeTimeTxt = (TextView)v.findViewById(R.id.modeTimeTxt);
modeTagTxt = (TextView)v.findViewById(R.id.modeTagTxt);
mainScroll = (ScrollView)v.findViewById(R.id.HAT1);
selectedScroll = (ScrollView)v.findViewById(R.id.HAT2);
tag = new OtherHelper().arrayread();
mainHashTag.setVisibility(View.GONE);
selectedHashTag.setVisibility(View.GONE);
clickListners();
}
public void clickListners(){
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
searchclear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTimeTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
modeTagTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
So when I try using this code the onclicklistners are not working.
What is the best way to do this.
Thanks
Write an Interface in your class,
class MyNewViewInit{
public interface ClickListenerInterface{
void onCLickSomething(Something something);
}
ClickListenerInterface mClickListener;
...
public MyNewViewInit(View v,ClickListenerInterface clickListener){
mClickListener = clickListener;
...
}
public void clickListners(){
...
backbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mClickListener!=null){
mClickListener.onCLickSomething(v);
}else{
//throw an error that activity should implement ClickListenerInterface
}
}
});
}
}
In your Activity,
class MyActivity extends Activity implements ClickListenerInterface{
public void onCreate(){
bb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new MyNewViewInit(CurrentContentView,this);
//if using Android Studio, then use the context menu to implement the interface
}
});
}
void onCLickSomething(Something something){
//do something with something
}
}
You can try do a abstract class extends Activity, like :
public abstract class MyInitActivity extends Activity
{
EditText editText;
TextView textView;
#Override
public void setContentView(int layoutResID)
{
super.setContentView(layoutResID);
editText = (EditText)findViewById(R.id.editText);
textView = (TextView)findViewById(R.id.textView);
}
}
and in your main activity you can extend your abstract class, like :
public class MainActivity extends MyInitActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/*********** Listener ************/
public void onButtonClick(View v)
{
textView.setText(editText.getText().toString());
}
}
you can also implement onClick() in the abstract class
i don't know if its a good practice , i prefer do the methods with view arguments and set this methods in the layout.xml file .
I had use this in one of code
STEP 1
Declare button in layout(XML file)
STEP 2
Declare all ur button like this--
Button btn1 = (Button)findViewById(R.id.btn1);
Button btn2 = (Button)findViewById(R.id.btn2);
Button btn3 = (Button)findViewById(R.id.btn3);
............so on
STEP 3
implement View.OnClickListener
Now after step 2 do this
btn1.setOnClickListener(this);
.
.
.
same way for all
STEP 4:
Use switch case inside Onclick
public void OnClick(View v)
{
case:R.id.btn1{
//ur command similarly use multiple case stmt for many button
}
}
I am making my own game for practising.
Once my game became bigger and bigger i noticed some delay once the main activity was about load.
So after reading, some threads i come up with another activity ( main ) showing a splash screen until the real main (menu) activity is loaded.
That worked pretty well, but since my game is on another activity once i change activity to play the game and then press the back button, or even when i am calling again the real main (menu) activity i face again this shitty lag/delay.
So to summurize, i have 3 activities so far.
1) The main activity which loads the splash screen and then calls the MenuActivity
2) The MenuActivity which basicly provides settings and buttons to start
3) The GameActivity which is the game
I could easily remove the MenuActivity and mix up the game with the menu activity handling the menu with the visibility of the images/buttons but i am wondering why is this happening for future knowleque also.
Here's the MenuActivity that makes the delay
public class MainActivity extends AppCompatActivity {
RelativeLayout _layer = null;
SharedPreferences _res;
Boolean _onFirstLoad=true,_bgMusic,_doubleBackToExitPressedOnce = false;
ImageView _musicBtnStop, _musicBtnStart, _about, _infoBtn;
SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(_onFirstLoad)
{
loadAllContent();
_onFirstLoad = false;
}
if (_bgMusic) {
_musicBtnStart.setVisibility(View.INVISIBLE);
BackGroundMusic.playMusic(); //starting
} else {
_musicBtnStart.setVisibility(View.VISIBLE);
_musicBtnStop.setVisibility(View.INVISIBLE);
}
//Start Game Button
_layer.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
callGameStart();
return false;
}
return false;
}
});
_musicBtnStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
callMusicStop();
}
});
_musicBtnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
callMusicStart();
}
});
_about.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
callGameStart();
}
});
}
#Override
public void onBackPressed() {
if (_doubleBackToExitPressedOnce) {
super.onBackPressed();
BackGroundMusic.quit();
return;
}
_doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
_doubleBackToExitPressedOnce=false;
}
}, 2000);
}
private void callGameStart() {
Intent game = new Intent(this, GameInterface.class);
startActivity(game);
}
private void callMusicStart() {
BackGroundMusic.setMuted(false);
_musicBtnStop.setVisibility(View.VISIBLE);
_musicBtnStart.setVisibility(View.INVISIBLE);
editor = _res.edit();
editor.putBoolean("bgMusic", true);
editor.apply();
}
private void callMusicStop() {
BackGroundMusic.setMuted(true);
_musicBtnStop.setVisibility(View.INVISIBLE);
_musicBtnStart.setVisibility(View.VISIBLE);
editor = _res.edit();
editor.putBoolean("bgMusic", false);
editor.apply();
}
private void loadAllContent() {
BackGroundMusic.setParams(MainActivity.this, R.raw.background_sound); //setting the music file
_layer = (RelativeLayout) findViewById((R.id.main));
_res = getApplicationContext().getSharedPreferences("com.nvm.tapper.prefs.xml", 0); // load our xml database
_bgMusic = _res.getBoolean("bgMusic", true);
_musicBtnStop = (ImageView) findViewById(R.id.stopMusic);
_musicBtnStart = (ImageView) findViewById(R.id.startMusic);
_about = (ImageView) findViewById(R.id.about);
}
I want to develop media player type of application which has two button one for play and pause and one for stop. I take two image buttons in my layout file and reference it in Java file and code like this which I put here, but when I click on stop button audio is getting stopped but then I want replay audio file; but I cant play with play button.
my Java code below
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_aarti_fragment, container, false);
btnplay=(ImageButton)v.findViewById(R.id.btnplay);
btnstop=(ImageButton)v.findViewById(R.id.btnstop);
seekbar=(SeekBar)v.findViewById(R.id.seekbar);
mp = MediaPlayer.create(getActivity(), R.raw.arti);
btnstop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mp.stop();
btnplay.setImageResource(R.drawable.ic_action_play);
Toast.makeText(getActivity(), "Stop",Toast.LENGTH_LONG).show();
}
});
btnplay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(mp.isPlaying())
{
mp.pause();
btnplay.setImageResource(R.drawable.ic_action_play);
Toast.makeText(getActivity(), "Pause",Toast.LENGTH_SHORT).show();
}
else
{ btnplay.setImageResource(R.drawable.ic_action_pause);
mp.start();
Toast.makeText(getActivity(), "Play",Toast.LENGTH_SHORT).show();
}
}
});
return v;
}
Use This:
private BackgroundSound mBackgroundSound;
private MediaPlayer player;
public class BackgroundSound extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
player = MediaPlayer.create(HomePage.this, R.raw.background);
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
player.setLooping(true); // Set looping
player.setVolume(volume, volume);
player.start();
return null;
}
}
For Playing:
mBackgroundSound = new BackgroundSound();
mBackgroundSound.execute();
For Stop:
mBackgroundSound.cancel(true);
if (player != null) {
player.stop();
player.release();
}
For Pause:
if (player != null) {
player.pause();
}
Try this it will help you....
//Click Listener on Image Button with play and Pause method
PLAY.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Play-Pause();
}
});
//Play Pause method...........
public void Play-Pause(){
if (mediaPlayer.isPlaying()) {
PLAY.setImageResource(R.drawable.pause);
mediaPlayer.pause();
} else {
PLAY.setImageResource(R.drawable.play);
mediaPlayer.start();
}
}
Once a media player is stopped, you need to call prepare on it again to get it to a state where you can call start(). Look at the state diagram here http://developer.android.com/reference/android/media/MediaPlayer.html#pause%28%29
Check this code, its better and simple solution !
public class MainActivity extends AppCompatActivity {
MediaPlayer mp;
Switch sw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = MediaPlayer.create(this,R.raw.nokia);
sw = findViewById(R.id.sw);
sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mp.setLooping(true);
}
});
}
public void play(View v){
mp.start();
}
public void pause(View v){
if(mp.isPlaying())
mp.pause();
}
public void stop(View v){
if(mp.isPlaying()) {
boolean loop=mp.isLooping();
mp.stop();
mp = MediaPlayer.create(this, R.raw.nokia);
mp.setLooping(loop);
}
}
}
I'm developing a game for android, and currently I'm doing the menu part of the program.
But I have a little problem.
If I add a setOnClickListener to the program, the program crashes.
<!-- language: lang-java -->
public class MakeLoveMenu extends Activity {
/* 0 = New 1 = Load 2 = Statistics 3 = Exit */
Button[] buttons;
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_make_love_menu);
buttonListeners();
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.make_love_menu, menu);
return true;
}
public void buttonListeners() {
buttons[0] = (Button) findViewById(R.id.button_StartNewGame);
buttons[1] = (Button) findViewById(R.id.button_ContinueGame);
buttons[2] = (Button) findViewById(R.id.button_Stats);
buttons[3] = (Button) findViewById(R.id.button_Exit);
buttons[0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
System.exit(0);
}
});
buttons[1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
System.exit(0);
}
});
buttons[2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
System.exit(0);
}
});
buttons[3].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
System.exit(0);
}
});
}
}
Can you find any errors? Maybe there's a problem with the array handling?
Thanks for your answer!
Based on your code, Button[] buttons is never instantiated, so, you would need something like:
Button[] buttons = new Button[4];
You can't access an array position that doesn't exist, since in your method buttonListeners() you are considering the existence of 4 positions (0 to 3), you need to create them before accessing.