I am beginner in development (my first project) please help! When I start new activity called fromdkk which was chosen from activity_main by pressing a button without any functions it works, but when I try to actually do something on that new activity, for example to run init(); it instantly stops. What should i change?
import...
public class fromdkk extends AppCompatActivity implements View.OnClickListener{
private Button USD,CAD,EUR,GBP;
private EditText insert;
private TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fromdkk);
init();
}
private void init() {
USD=(Button)findViewById(R.id.usd);
CAD=(Button)findViewById(R.id.cad);
EUR=(Button)findViewById(R.id.eur);
GBP=(Button)findViewById(R.id.gbp);
insert=(EditText)findViewById(R.id.ins);
result=(TextView)findViewById(R.id.res);
USD.setOnClickListener(this);// on click do this which is stated later as a function
EUR.setOnClickListener(this);
CAD.setOnClickListener(this);
GBP.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String num1=insert.getText().toString();//get text from user
float usd1= (float) 0.142;
float eur= (float) 0.134;
float cad= (float) 0.192;
float gbp= (float) 0.114;
switch(view.getId()) {//switch between an cases regarding Ids, execute operation which matches button id
case R.id.usd:
float curr = Float.parseFloat(num1) * Float.parseFloat(String.valueOf(usd1));
result.setText(String.valueOf(curr) + " $");
break;
case R.id.cad:
float curr2 = Float.parseFloat(num1) * Float.parseFloat(String.valueOf(cad));
result.setText(String.valueOf(curr2) + " $");
break;
case R.id.eur:
float curr3 = Float.parseFloat(num1) * Float.parseFloat(String.valueOf(eur));
result.setText(String.valueOf(curr3)+ " €");
break;
case R.id.gbp:
float curr4 = Float.parseFloat(num1) * Float.parseFloat(String.valueOf(gbp));
result.setText(String.valueOf(curr4) + " £");
break;
app log: --------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.martin.currencyconverter, PID: 2381 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.martin.currencyconverter/com.example.martin.currencyconverter.fromdkk}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
logcat:11-26 16:28:41.191 2247-2438/com.example.martin.currencyconverter I/OpenGLRenderer: Initialized EGL, version 1.4 11-26 16:28:41.191 2247-2438/com.example.martin.currencyconverter D/OpenGLRenderer: Swap behavior 1 11-26 16:28:41.316 2247-2438/com.example.martin.currencyconverter E/EGL_emulation: tid 2438: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH) 11-26 16:28:41.316 2247-2438/com.example.martin.currencyconverter W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9f9fec20, error=EGL_BAD_MATCH