Lieberta
Lieberta

Reputation: 147

Android Toast doesn't pop up

My problem statement::> I have a button, a text field and I want a toast to pop out after I give input in the text field and click the button. However, after giving the input and click the button, the toast does not pop up. Instead, the application close down. If I remove the text field, the toast will come out just fine.

So I think, the one giving me error is the text field.

Here is my code for the text field in the java:::

    public String getMyString(){

    EditText edit1=(EditText) findByViewId(R.id.editText1);
    String string1 = edit1.getText().toString();
    return string1;

    }

I will return this to the onClick method as:::

        public void onClick(View view){
        String string = getMyString();
        Toast.makeText(this, string,Toast.LENGTH_SHORT).show(); 
        }

In my XML file for editText

       <EditText
        android:id="@+id/editText1"
        android:labelFor="@id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="38dp"
        android:ems="10"
        android:inputType="text"
       />

This is my second day learning eclipse so I will appreciate less technical jargon. I hope you guys can help me because I try to google but I really don't understand anything... XD Thanks.

Upvotes: 0

Views: 157

Answers (10)

Pihu
Pihu

Reputation: 1039

Try this:

   public void onClick(View view){
    String string = getMyString();
    Toast.makeText(YourActivityName.this, string,Toast.LENGTH_SHORT).show(); 
    }

Upvotes: 0

Top Cat
Top Cat

Reputation: 2483

You can do like this. Use getActivity() in the Toast.

public void onClick(View view){
String string = getMyString();
Toast toast=Toast.makeText(getActivity(), string , Toast.LENGTH_SHORT);
toast.show(); }

Upvotes: 0

FMontano
FMontano

Reputation: 927

Just make sure that your Activity (or Fragment) is implementing the interface OnClickListener

public class MyActivity implements View.OnClickListener { ... 

Then, you set your textview's listener to the current activity

edit1.setOnClickListener(this);

And finally, make sure you use the activity's this reference, as suggested by many users here:

@Override
public void onClick(View view){
    String string = getMyString();
    Toast.makeText(MyActivity.this, string,Toast.LENGTH_SHORT).show(); 
}

Upvotes: 0

Lucifer
Lucifer

Reputation: 29642

this always refers to current object, hence inside the onClick() method , this refers to Listener you have implemented.

While Toast's makeText() method accepts argument as makeText(Context context, CharSequence text, int duration)

Here you can pass context value as

  • getApplicationContext(),
  • getBaseContext() or by giving
  • ActivityName.this like below,

    public void onClick(View view)
    {
         String string = getMyString();
         Toast.makeText( getApplicationContext(), string,Toast.LENGTH_SHORT).show(); 
         //Toast.makeText( getBaseContext(), string,Toast.LENGTH_SHORT).show(); 
         //Toast.makeText( ActivityName.this, string,Toast.LENGTH_SHORT).show(); 
    }
    

Upvotes: 0

CodeWalker
CodeWalker

Reputation: 2378

Cross check that you are putting the toast under OnClickListener. Then, please try this.

public void onClick(View view){
String string = getMyString();
Toast toast=Toast.makeText(MainActivity.this, string , Toast.LENGTH_SHORT);
toast.show();

Upvotes: 0

Namrata
Namrata

Reputation: 1684

Try this:-

public void onClick(View view){
    String string = getMyString();
    Toast.makeText(YoursActivity.this, string,Toast.LENGTH_SHORT).show(); 
    }

And make sure that your button is implemented to OnclickListener.

Upvotes: 0

Arun Antoney
Arun Antoney

Reputation: 4382

You have to initialize the edittext before button click .So that entered text can retrieve on button click . Now null pointer exception will occur . So please try like that

Put it in oncreate

    EditText edit1=(EditText) findByViewId(R.id.editText1);

Upvotes: 1

Jitesh Upadhyay
Jitesh Upadhyay

Reputation: 5260

try with the given code , use YoursActivity.this instead of this

 public void onClick(View view){
    String string = getMyString();
    Toast.makeText(YoursActivity.this, string,Toast.LENGTH_SHORT).show(); 
    }

Upvotes: 1

Giridharan
Giridharan

Reputation: 4462

Try this..it would help you..

public void onClick(View view){
    String string = getMyString();
    Toast.makeText(classname.this, string,Toast.LENGTH_SHORT).show(); 
    }

Upvotes: 0

Sekhar Madhiyazhagan
Sekhar Madhiyazhagan

Reputation: 889

    public void onClick(View view){
    String string = getMyString();
    Toast.makeText(getApplicationContext(), string,Toast.LENGTH_SHORT).show(); 
    }

Upvotes: 0

Related Questions