Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hello i have 2 activities in android, and i want to go from one activity to another using a button. The main activity is:

public class AppActivity extends Activity
{
Button button; 

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

public void addListenerOnButton() {

    final Context context = this;

    button = (Button) findViewById(R.id.button1); 

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {  

            Intent intent = new Intent(context, App2Activity.class);
            startActivity(intent);
        }

    });

}

}

And it works just fine, but then i have the second activity that is supposed to call the first activity but does nothing:

public class App2Activity extends Activity implements OnClickListener {

Button button, button_n1, button_n2, button_n3, button_n4, button_n5, button_n6, button_n7, button_n8, button_n9, button_n0, button_clear, button_div, button_mult, button_mais, button_menos, button_igual, button_pt;

EditText textcalc;

@Override
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
    textcalc = (EditText) findViewById(R.id.textcalc);
    Float num1, num2, total;

    button = (Button) findViewById(R.id.button2); 
    button_n0 = (Button) findViewById(R.id.button_n0);
    button_n1 = (Button) findViewById(R.id.button_n1);
    button_n2 = (Button) findViewById(R.id.button_n2);
    button_n3 = (Button) findViewById(R.id.button_n3);
    button_n4 = (Button) findViewById(R.id.button_n4);
    button_n5 = (Button) findViewById(R.id.button_n5);
    button_n6 = (Button) findViewById(R.id.button_n6);
    button_n7 = (Button) findViewById(R.id.button_n7);
    button_n8 = (Button) findViewById(R.id.button_n8);
    button_n9 = (Button) findViewById(R.id.button_n9);
    button_clear = (Button) findViewById(R.id.button_clear);
    button_mult = (Button) findViewById(R.id.button_mult);
    button_div = (Button) findViewById(R.id.button_div);
    button_mais = (Button) findViewById(R.id.button_mais);
    button_menos = (Button) findViewById(R.id.button_menos);
    button_igual = (Button) findViewById(R.id.button_igual);
    button_pt = (Button) findViewById(R.id.button_pt);

    // set a listener
    button_div.setOnClickListener(this);
    button_mult.setOnClickListener(this);
    button_mais.setOnClickListener(this);
    button_menos.setOnClickListener(this);
    button_clear.setOnClickListener(this);
    button_igual.setOnClickListener(this);
    button_pt.setOnClickListener(this);
    button.setOnClickListener(this);
    button_n0.setOnClickListener(this);
    button_n1.setOnClickListener(this);
    button_n2.setOnClickListener(this);
    button_n3.setOnClickListener(this);
    button_n4.setOnClickListener(this);
    button_n5.setOnClickListener(this);
    button_n6.setOnClickListener(this);
    button_n7.setOnClickListener(this);
    button_n8.setOnClickListener(this);
    button_n9.setOnClickListener(this);



}


@Override
public void onClick(View v) {
    String texto;
    texto = textcalc.getText().toString();
    Boolean numeros_existem = texto.endsWith("0") || texto.endsWith("1") || texto.endsWith("2") || texto.endsWith("3") || texto.endsWith("4") || texto.endsWith("5") || texto.endsWith("6") || texto.endsWith("7") || texto.endsWith("8") || texto.endsWith("9");

    switch (v.getId()) {
        case R.id.button://n?o está a funcionar
            Intent intent2 = new Intent(App2Activity.this, AppActivity.class);
            startActivity(intent2);//vai come?ar a activity que é definida no 2oparametro de intent
            break;
        //many more cases...

    }
}

}

Does someone has a clue why it doen't work?


SOLVED

It seems i was seaching for something that didn't exit. I changed the case to:

switch (v.getId()) {
        case R.id.button2://this is the correct id
            Intent intent2 = new Intent(App2Activity.this, AppActivity.class);
            startActivity(intent2);//vai come?ar a activity que é definida no 2oparametro de intent
            break;
        //many more cases...

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
178 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your intent and start activity code are fine.

Rather, your problem is that you are testing to see if the identity of your clicked view matches "R.id.button". Yet, you have not set your activity class to be the on click listener of any view with that ID, so your the code that would start a new activity will never run.

Change your case statement to check for a match with one or more of the views that you have actually set your activity to be the listener for (or set it to be one for R.id.button), and it will work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...