-2

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

quant
  • 493
  • 9
  • 21
  • Add the logcat to the question please, and verify that the Activity you want to start is in the manifest – OneCricketeer Nov 26 '16 at 15:56
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Henry Nov 27 '16 at 09:09
  • it is in the manifest, error: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference – quant Nov 27 '16 at 09:21
  • can you help @Henry? – quant Nov 27 '16 at 09:31
  • Make sure the buttons are really defined in the layout with the given IDs. – Henry Nov 27 '16 at 10:59

1 Answers1

0

you have to initialize the button view first for example

if in your xml you defined Button like this

            <TextView
                android:id="@+id/mybtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="16dp"
                android:layout_marginTop="16dp"
                android:text="Click me!"
                />

in your Activity

Button b = (Button) findViewById(R.id.mybtn);

after this line Button gets innitialized then only you can add listners to it

if you are in a *Fragment* you have write

Button b = (Button) rootView.findViewById(R.id.mybtn);

where rootView is assumed to be the inflated fragments view.

Jayanth
  • 5,954
  • 3
  • 21
  • 38