Snehasis Panda
Snehasis Panda

Reputation: 75

App crashes when I run this

my app crashes when I press a button with android:onClick"toastExpert"

public void toastExpert(){

    toastMe(null);
}

public void toastMe(View view) {

    CharSequence text = "Hello Myself Dristi!";
    int duration = Toast.LENGTH_SHORT;
    Toast.makeText(getApplicationContext(), text, duration).show();
}

Anyone please help... The logcat details

enter image description here

Upvotes: 0

Views: 70

Answers (3)

Cimoe
Cimoe

Reputation: 586

Your public method toastExpert() is wrong. You have to add following parameters:

public void toastExpert(View view){

    toastMe(null);
}

Note that every method you use in android:onClick has to look like this

public void yourOnClickMethod(View view){
    doSomething();
}

Upvotes: 1

Lalit Jadav
Lalit Jadav

Reputation: 1427

Try

Toast.makeText(view.getContext(), text, duration).show();

Upvotes: 0

Suraj Makhija
Suraj Makhija

Reputation: 1396

You can go for android:onClick="toastMe" in your button in your xml

Upvotes: 0

Related Questions