I am learning android. Every time I run the app it is crashing. I can't seem to find any error. Help needed.
Here is the main code.
MainActivity.java
package com.example.arnab.myfirstapplication;
import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
Button btnAdd = (Button)findViewById(R.id.btnAdd);
EditText num1 = (EditText)findViewById(R.id.num1);
EditText num2 = (EditText)findViewById(R.id.num2);
TextView result = (TextView)findViewById(R.id.result);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v == btnAdd){
int n1 = Integer.parseInt(num1.getText().toString());
int n2 = Integer.parseInt(num2.getText().toString());
int sum = n1 + n2;
result.setText(Integer.toString(sum));
}
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}
Please help is very much appreciated. I am stuck at this stage. No matter what I do, the app crashes.
Update I did not know that it is not possible to use findViewById before setcontentview . at that time my logic was not clear. Sorry about this.