mardi 8 avril 2014

Java - Toast Android ne pop up - Stack Overflow


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.




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();
}



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);



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.




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



Try this..it would help you..


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



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();



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();
    }




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();
}



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(); }


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.



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();
}


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);


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.



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


Try this..it would help you..


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


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();


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();
    }



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();
}


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(); }

0 commentaires:

Enregistrer un commentaire