Zejay
Zejay

Reputation: 3

Unfortunately app has stopped Android Studio

I am new in programming with android studio. I have an application but it stopps when I want to start it. At first I had only one Button and one onclicklistener, everthing was working fine. Since I have a second Button with onlicklistener the app stopped unfortunately. Whats wrong with my code? Thanks for help.

Main Activity:

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import de.example.einheitenumrechnergradfahrenheit.R;

public class MainActivity extends ActionBarActivity {

    Button berechnenbutton;
    Button button1;
    EditText eingabegrad;
    TextView ergebnisanzeige;
    double ergebnis;




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

        setContentView(R.layout.activity_main);


        eingabegrad = (EditText) findViewById(R.id.editText1);
        ergebnisanzeige = (TextView) findViewById(R.id.textView3);
        berechnenbutton = (Button) findViewById(R.id.button);
        berechnenbutton.setOnClickListener((OnClickListener) this);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener((OnClickListener) this);}



            public void onClick(View v) {

                switch (v.getId()) {


                    case R.id.button:
                        if (eingabegrad.length() == 0) {
                            ergebnisanzeige.setText("Keine Eingabe erfolgt");
                        } else {
                            //Formel: Grad Celsius in Fahrenheit = ((Celsius *9)/5)+32
                            ergebnis = ((Double.parseDouble(eingabegrad.getText().toString()) / 1.8) - 32);
                            ergebnisanzeige.setText(eingabegrad.getText() + "  Grad Fahrenheit sind"
                                    + " umgerechnet " + Double.toString(ergebnis) + " Grad Celsius");
                        }
                        break;


                    case R.id.button1:
                        if (eingabegrad.length() == 0) {
                            ergebnisanzeige.setText("Keine Eingabe erfolgt");
                        } else {
                            //Formel: Grad Celsius in Fahrenheit = ((Celsius *9)/5)+32
                            ergebnis = ((Double.parseDouble(eingabegrad.getText().toString()) * 1.8) + 32);
                            ergebnisanzeige.setText(eingabegrad.getText() + "  Grad Celsius sind"
                                    + " umgerechnet " + Double.toString(ergebnis) + " Grad Fahrenheit");
                        }
                        break;



                    default:
                        break;
                }

            };


        }

Upvotes: 0

Views: 542

Answers (1)

Thomas R.
Thomas R.

Reputation: 8073

Your activity should implement the OnCLickListener and your onClick method must be annotated with @Override.

Try this:

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

    //omitted for clarity


    @Override
    public void onClick(View v) {

        //omitted for clarity

    }

Upvotes: 1

Related Questions