Question

I got a common problem, about my Toast does not appearing. I've searched it on the site many times, and tried some different things. However - it still doesn't work.

My Code:

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class Welcome extends Activity implements OnClickListener {

EditText efn,eln,eage;
Button submit;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);

    efn=(EditText)findViewById(R.id.fname);
    eln=(EditText)findViewById(R.id.lname);
    eage=(EditText)findViewById(R.id.age);

    submit=(Button)findViewById(R.id.submit);
    submit.setOnClickListener(this);

}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    Toast.makeText(Welcome.this, "Please complete all the details", Toast.LENGTH_SHORT).show();

}

The xml file of welcome:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<TextView
    android:id="@+id/welcome_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/fname"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="26dp"
    android:layout_marginTop="25dp"
    android:text="Welcome" />

<EditText
    android:id="@+id/fname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/welcome_text"
    android:layout_marginLeft="26dp"
    android:layout_marginTop="37dp"
    android:ems="10"
    android:hint="First name"
    android:inputType="textPersonName" />

<EditText
    android:id="@+id/lname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/fname"
    android:layout_below="@+id/fname"
    android:layout_marginTop="54dp"
    android:ems="10"
    android:hint="Last name"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/age"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/lname"
    android:layout_below="@+id/lname"
    android:layout_marginTop="40dp"
    android:ems="10"
    android:hint="Age"
    android:textDirection="ltr"
    android:inputType="number" />


 <Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="24dp"
    android:text="Submit" />

Instead of Welcome.this I've tried also this and getApplicationContext().

The result of the code is openning the screen and after pressing the button - nothing happend.

I have no other option.. therefore I ask you guys for help.

Thanks in advance,

Yaniv.

The error log according to @jmsalcido answer:

01-25 17:57:07.367: D/gralloc_goldfish(3248): Emulator without GPU emulation detected.
01-25 17:57:08.387: I/Choreographer(3248): Skipped 48 frames!  The application may be doing too much work on its main thread.
01-25 17:57:08.768: D/AndroidRuntime(3248): Shutting down VM
01-25 17:57:08.768: W/dalvikvm(3248): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
01-25 17:57:08.798: E/AndroidRuntime(3248): FATAL EXCEPTION: main
01-25 17:57:08.798: E/AndroidRuntime(3248): java.lang.IllegalStateException: Could not find a method showToast(View) in the activity class com.example.idiagnostician1.MainActivity for onClick handler on view class android.widget.Button with id 'submit'
01-25 17:57:08.798: E/AndroidRuntime(3248):     at android.view.View$1.onClick(View.java:3586)
01-25 17:57:08.798: E/AndroidRuntime(3248):     at android.view.View.performClick(View.java:4204)
01-25 17:57:08.798: E/AndroidRuntime(3248):     at android.view.View$PerformClick.run(View.java:17355)
01-25 17:57:08.798: E/AndroidRuntime(3248):     at android.os.Handler.handleCallback(Handler.java:725)
01-25 17:57:08.798: E/AndroidRuntime(3248):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 17:57:08.798: E/AndroidRuntime(3248):     at android.os.Looper.loop(Looper.java:137)
01-25 17:57:08.798: E/AndroidRuntime(3248):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-25 17:57:08.798: E/AndroidRuntime(3248):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 17:57:08.798: E/AndroidRuntime(3248):     at java.lang.reflect.Method.invoke(Method.java:511)
01-25 17:57:08.798: E/AndroidRuntime(3248):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-25 17:57:08.798: E/AndroidRuntime(3248):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-25 17:57:08.798: E/AndroidRuntime(3248):     at dalvik.system.NativeStart.main(Native Method)
01-25 17:57:08.798: E/AndroidRuntime(3248): Caused by: java.lang.NoSuchMethodException: showToast [class android.view.View]
01-25 17:57:08.798: E/AndroidRuntime(3248):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
01-25 17:57:08.798: E/AndroidRuntime(3248):     at java.lang.Class.getMethod(Class.java:915)
01-25 17:57:08.798: E/AndroidRuntime(3248):     at android.view.View$1.onClick(View.java:3579)
01-25 17:57:08.798: E/AndroidRuntime(3248):     ... 11 more
Was it helpful?

Solution

Are you using a simulator or running it in a device?

Nothing seems wrong but try if this works for you, its the same but instead of implementing OnClickListener and the override try this

submit.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
    Toast.makeText(Welcome.this, "Please complete all the details", Toast.LENGTH_SHORT).show();
    }
    });

and leave the class like this

public class Welcome extends Activity

and remove

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    Toast.makeText(Welcome.this, "Please complete all the details", Toast.LENGTH_SHORT).show();

}

OTHER TIPS

You need to put this check in OnClick

if (v.getId() == R.id.submit) {
  Toast.makeText(Welcome.this, "Please complete all the details", Toast.LENGTH_SHORT).show();
}

When you implement an OnClickListener, the onClick() method consumes a View. When performing any actions in the onClick(), we need to check on which View is it called on. In a case where there may be more than one button (or more than one view to click on), this helps identify on what View are we calling the onCLick on. Hence the check if (v.getId() == R.id.submit) is important.

According to this Difference between Activity Context and Application Context you should use Activity.this not getApplicationContext()

always go to developers.android.com: http://developer.android.com/guide/topics/ui/controls/button.html#ClickListener

do not Override the onClick method, use an onClick listener there, this should work, this is your actual file:

 <Button
  android:id="@+id/submit"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_centerHorizontal="true"
  android:layout_marginBottom="24dp"
  android:text="Submit" />

add an android:onClick attribute there to look like this:

 <Button
  android:id="@+id/submit"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_centerHorizontal="true"
  android:layout_marginBottom="24dp"
  android:onClick="showToast"
  android:text="Submit" />

and in your Activity:

public void showToast(View v) {
    Toast.makeText(getApplicationContext(), "Please complete all the details", Toast.LENGTH_SHORT).show();

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top