开始没有使用Java的活动

时间:2015-04-16 17:42:00

标签: java android

你好我在android中有2个活动,我想用一个按钮从一个活动转到另一个活动。  主要活动是:

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);
        }

    });

}

}

它工作正常,但是我有第二个活动应该调用第一个活动,但什么都不做:

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 2ºparametro de intent
            break;
        //many more cases...

    }
}

}

有人知道为什么它不起作用吗?


解决

似乎我正在寻找一些没有退出的东西。我把案子改为:

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 2ºparametro de intent
            break;
        //many more cases...

    }

1 个答案:

答案 0 :(得分:2)

您的意图和开始活动代码没问题。

相反,您的问题是您正在测试您点击的视图的标识是否与“R.id.button”匹配。但是,您尚未将活动类设置为具有该ID的任何视图的单击侦听器,因此启动新活动的代码将永远不会运行。

更改您的案例陈述,以检查您实际上将您的活动设置为监听器的一个或多个视图的匹配(或将其设置为R.id.button的一个),它将起作用