应用程序在启动新活动时不断崩溃(按钮点击)

时间:2015-03-27 08:25:06

标签: java android android-intent android-activity

应用程序不断在一项新活动中崩溃。主要活动启动后,我按任意按钮,应用程序停止或崩溃。

我不知道我可能做得不对,但这是我的mainActivity代码 我正在使用android studio

public class Calculator  extends ActionBarActivity{

    public Button loan;
    public Button exit;



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

    }

    public void addListenerOnButton() {

        final Context context = this;

        loan = (Button) findViewById(R.id.loans);
        loan.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

            Intent intent = new Intent(context, loancalculator.class);
            startActivity(intent);
            finish();


            }

        });

        exit = (Button) findViewById(R.id.exits);
        exit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context, presentvaluecalculator.class);
                startActivity(intent);
                finish();


            }

        });
    }

}

1 个答案:

答案 0 :(得分:0)

        public class Calculator  extends ActionBarActivity{

            public Button loan,exit;
            Calculator context;


            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.calc);
                context = this;
                addListenerOnButton();

            }

            public void addListenerOnButton() {



                loan = (Button) findViewById(R.id.loans);
                loan.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {

                    Intent intent = new Intent(context, loancalculator.class);
                    startActivity(intent);
                    finish();


                    }

                });

                exit = (Button) findViewById(R.id.exits);
                exit.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {

                        Intent intent = new Intent(context, presentvaluecalculator.class);
                        startActivity(intent);
                        finish();


                    }

                });
            }

        }



   // And make sure you have declared all the activities in manifest

        <activity android:name="com.yourpackage.loancalculator"/>
        <activity  android:name="com.yourpackage.presentvaluecalculator"/>