lunar
lunar

Reputation: 1232

OnClickListener - how to implement correctly?

OK so here is some code. Seems to have no errors however when run in Android Studio I am getting "Unfortunately, NameOfApplication has stopped". I have no idea what is wrong with it.

public class MainActivity extends Activity implements OnClickListener {

EditText centimeters = (EditText) findViewById(R.id.editCentimeters);
EditText inches = (EditText) findViewById(R.id.editInches);
Button btnConverter = (Button) findViewById(R.id.button);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnConverter.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View view) {

    switch(view.getId()){
        case R.id.button:
            double c = Double.valueOf(centimeters.getText().toString());
            double i = c *  0.393701;
            inches.setText(String.valueOf(i));
            break;
        default:
            break;
    }
}
}

I have another question as well. It is my beginning with Android and the reference api is quite big so what would you recommend as a best and quickest way to learn it? Get a basic understanding of its structure etc.?

Upvotes: 0

Views: 10398

Answers (2)

jpardogo
jpardogo

Reputation: 5666

public class MainActivity extends Activity implements OnClickListener {

   //You need the global variables to access the data from onClick, altough you could findViewById inside the onClick itself not to have global variables that exist during the whole activity life.
   private EditText centimeters;
   private EditText inches;

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       //Here you set the view (setContentView(R.layout.activity_main)) so after this line is when you can start looking for your views declared in the xml layout file, "activity_main.xml
       setContentView(R.layout.activity_main);
       //We can look for the views form here
       centimeters = (EditText) findViewById(R.id.editCentimeters);
       inches = (EditText) findViewById(R.id.editInches);
       Button btnConverter = (Button) findViewById(R.id.button);

       btnConverter.setOnClickListener(this);
   }

   ....

The rest the same

Upvotes: 1

Ahmad
Ahmad

Reputation: 72673

You have to initialise UI elements after calling setContentView() in the onCreate() method like this:

EditText centimeters;
EditText inches;
Button btnConverter;

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

    centimeters = (EditText) findViewById(R.id.editCentimeters);
    inches = (EditText) findViewById(R.id.editInches);
    btnConverter = (Button) findViewById(R.id.button);

    btnConverter.setOnClickListener(this);
}

Upvotes: 6

Related Questions