启动活动不灵爪哇 [英] Start of activity not working Java

查看:176
本文介绍了启动活动不灵爪哇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有2个活动的机器人,我想去从一个活动到另一个使用一个按钮。
 主要活动是:

 公共类AppActivity扩展活动
{
Button按钮;@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    addListenerOnButton();
}公共无效addListenerOnButton(){    最后上下文的背景下=这;    按钮=(按钮)findViewById(R.id.button1);    button.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(查看为arg0){            意向意图=新意图(背景下,App2Activity.class);
            startActivity(意向);
        }    });}}

和它工作得很好,但后来我有一个应该调用的第一个活动,但什么都不做的第二个活动:

 公共类App2Activity扩展活动实现OnClickListener {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;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main2);
    textcalc =(EditText上)findViewById(R.id.textcalc);
    浮动NUM1,NUM2,总;    按钮=(按钮)findViewById(R.id.button2);
    button_n0 =(按钮)findViewById(R.id.button_n0);
    button_n1 =(按钮)findViewById(R.id.button_n1);
    button_n2 =(按钮)findViewById(R.id.button_n2);
    button_n3 =(按钮)findViewById(R.id.button_n3);
    button_n4 =(按钮)findViewById(R.id.button_n4);
    button_n5 =(按钮)findViewById(R.id.button_n5);
    button_n6 =(按钮)findViewById(R.id.button_n6);
    button_n7 =(按钮)findViewById(R.id.button_n7);
    button_n8 =(按钮)findViewById(R.id.button_n8);
    button_n9 =(按钮)findViewById(R.id.button_n9);
    button_clear =(按钮)findViewById(R.id.button_clear);
    button_mult =(按钮)findViewById(R.id.button_mult);
    button_div =(按钮)findViewById(R.id.button_div);
    button_mais =(按钮)findViewById(R.id.button_mais);
    button_menos =(按钮)findViewById(R.id.button_menos);
    button_igual =(按钮)findViewById(R.id.button_igual);
    button_pt =(按钮)findViewById(R.id.button_pt);    //设置监听器
    button_div.setOnClickListener(本);
    button_mult.setOnClickListener(本);
    button_mais.setOnClickListener(本);
    button_menos.setOnClickListener(本);
    button_clear.setOnClickListener(本);
    button_igual.setOnClickListener(本);
    button_pt.setOnClickListener(本);
    button.setOnClickListener(本);
    button_n0.setOnClickListener(本);
    button_n1.setOnClickListener(本);
    button_n2.setOnClickListener(本);
    button_n3.setOnClickListener(本);
    button_n4.setOnClickListener(本);
    button_n5.setOnClickListener(本);
    button_n6.setOnClickListener(本);
    button_n7.setOnClickListener(本);
    button_n8.setOnClickListener(本);
    button_n9.setOnClickListener(本);}
@覆盖
公共无效的onClick(视图v){
    串texto;
    。texto = textcalc.getText()的toString();
    布尔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);    开关(v.getId()){
        案例R.id.button:// NAO ESTA一个funcionar
            意图int​​ent2 =新意图(App2Activity.this,AppActivity.class);
            startActivity(intent2); //奥钢联começar一个活动阙édefinida没有2ºparametro德意图
            打破;
        //更多的情况下...    }
}}

是否有人有线索,为什么它doen't工作?


解决

看来我还是seaching的东西,并没有退出。我改变的情况下:

 开关(v.getId()){
        案例R.id.button2://这是正确的ID
            意图int​​ent2 =新意图(App2Activity.this,AppActivity.class);
            startActivity(intent2); //奥钢联começar一个活动阙édefinida没有2ºparametro德意图
            打破;
        //更多的情况下...    }


解决方案

您的意图,并开始活动code是罚款。

相反,你的问题是,你正在测试,看看你的点击视图的身份相匹配R.id.button。然而,你有没有设置你的活动类是与该ID的任何观点点击监听器,让你的code,将开始一个新的活动将永远不会运行。

改变你的case语句来检查一个或多个您已实际设置您的活动成为听众的意见匹配(或将其设置为一个R.id.button),它会工作

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

    }

解决方案

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.

这篇关于启动活动不灵爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆