0

I finally learned about adding a button to a page and actually making it navigate to another activity "XML Page". Anyway, I have been trying to add 2 buttons in the same page which navigate each to a different XML's Pages. All I did was copy the first button which worked and then change the button name and all other things the first button works but the second isn't. It shows a click but nothing happens after. Back1 Button works. TMode Button does the trouble. Eclipse is not showing errors.

Here is my code -

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;

import android.view.View.OnClickListener;
import android.widget.Button;

public class GameMode extends Activity {

    /** Called when the activity is first created.*/
    Button btn;
    Button btn1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_game_mode);

        btn=(Button)findViewById(R.id.Back1);

        btn.setOnClickListener(btn2Listener);
    }

    private OnClickListener btn2Listener=new OnClickListener() {
        public void onClick(View v) {
            Intent intent2=new Intent(GameMode.this,MainActivity.class);

            startActivity(intent2);
        }
    };

    public void onCreate1(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_game_mode);

        btn=(Button)findViewById(R.id.TMode);
        btn.setOnClickListener(btn3Listener);
    }

    private OnClickListener btn3Listener=new OnClickListener() {
        public void onClick(View v) {
            Intent intent3=new Intent(GameMode.this,CharacterSelect.class);

            startActivity(intent3);
        }
    };
}

3 Answers 3

1

Try something like this:

import android.app.Activity; 
import android.content.Intent;
import android.os.Bundle; 
import android.view.View;
import android.view.View.OnClickListener; 
import android.widget.Button;

public class GameMode extends Activity {

Button btn1;
Button btn2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_mode);

    btn1=(Button)findViewById(R.id.Back1);
    btn1.setOnClickListener(btn1Listener);

    btn2=(Button)findViewById(R.id.TMode);
    btn2.setOnClickListener(btn2Listener);
}


private OnClickListener btn1Listener=new OnClickListener() {
    public void onClick(View v) {
            Intent intent1=new Intent(GameMode.this,MainActivity.class);
            startActivity(intent2);
        }
    };
private OnClickListener btn2Listener=new OnClickListener() {
    public void onClick(View v) {
            Intent intent1=new Intent(GameMode.this,CharacterSelect.class);
            startActivity(intent2);
        }
    };     
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should in your XML file define two buttons

<Button
   android:id="@+id/button1"
   ... />

<Button
   android:id="@+id/button2"
   ... />

And then in your Activity in onCreate() method you do

Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
    ...
})

Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
    ...
});

2 Comments

@ByteHamster i tried your method.. it now transfare to the other XML but for some reason the app crash once it transfare
Never Mind i solved it.. i had to delete some extra imports i had in the destination XML
0
import android.app.Activity; 
import android.content.Intent;

import android.os.Bundle; 
import android.view.View;

import android.view.View.OnClickListener; 
import android.widget.Button;

public class GameMode extends Activity {

Button btn1;

Button btn2;

@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_game_mode);

    btn1=(Button)findViewById(R.id.Back1);
    btn1.setOnClickListener(btn1Listener);

    btn2=(Button)findViewById(R.id.TMode);

    btn2.setOnClickListener(btn2Listener);
}


private OnClickListener btn1Listener=new OnClickListener() {

    public void onClick(View v) {

            Intent intent1=new Intent(GameMode.this,MainActivity.class);

            startActivity(intent1);
        }
    };
private OnClickListener btn2Listener=new OnClickListener() {

    public void onClick(View v) {

            Intent intent2=new Intent(GameMode.this,CharacterSelect.class);

            startActivity(intent2);
        }
    };     
}

2 Comments

Isn't this exactly what @ByteHamster suggested? You should have accepted his answer instead.
Yeah but i am new here i didnt know i have a way to accept the answer.. but done :) did that !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.