0
votes

The application gets crashed when i press the buttons. Each convert"country name" function is a button. I'll post the error in the comment. Thanks in advance.

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void convertIndia(View view){ convert(64.7,"Rupees"); }
    public void convertEurope(View view){ convert(0.85,"Euros"); }
    public void convertBritish(View view){ convert(0.78,"Pounds"); }
    public void convertUae(View view) { convert(3.67,"Dhiram"); }
    public void convertCanada(View view){ convert(1.26,"Ca Dollors"); }     
    public void convertAustralia(View view) { convert(1.26,"Aus Dollors"); }

    public void convert(double other,String country){

        EditText editText = (EditText) findViewById(R.id.value);
        Double usdValue = Double.parseDouble(editText.getText().toString());

        Log.i("USDVAL",usdValue.toString());
        Log.i("EDITTEXT",editText.toString());

        Double finalvalue = usdValue * other;

        Toast.makeText(MainActivity.this, String.format("%.2f", finalvalue) + " " + country, Toast.LENGTH_LONG).show();
    }
}

error log

08-26 12:04:46.140 2387-2387/com.fellowwebster.currencyconverter E/AndroidRuntime: FATAL EXCEPTION: main Process: com.fellowwebster.currencyconverter, PID: 2387 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:5637)  at android.view.View$PerformClick.run(View.java:22429)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.fellowwebster.currencyconverter.MainActivity.convert(MainActivity.java:38) at com.fellowwebster.currencyconverter.MainActivity.convertIndia(MainActivity.java:18) at java.lang.reflect.Method.invoke(Native Method)  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)  at android.view.View.performClick(View.java:5637)  at android.view.View$PerformClick.run(View.java:22429)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

Xml

<Button
    android:id="@+id/india"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/british"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/value"
    android:layout_toStartOf="@+id/british"
    android:onClick="convertIndia"
    android:text="IND_Rupee"
    tools:layout_editor_absoluteX="131dp"
    tools:layout_editor_absoluteY="70dp" />
1
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.fellowwebster.currencyconverter, PID: 2387 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)Dhanasekar
Please share your xml code @DhanasekarAnand Diamond
Please edit your question to provide the complete stack trace.Mike M.
Please edit your question to update or provide additional information. Please don't post it in comments.Mike M.
Ok, newbie here sorry. @MikeM.Dhanasekar

1 Answers

0
votes

You should try onClickListener instead of android:onClick. Example:

Button button = (Button) findViewById(R.id.yourButtonId);   
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        callYourMethodHere();
    }
});

Place this inside onCreate and you are good to go