2

I don't understand why my toast is crashing the app.

I have two sets of code, the one I originally did which is crashing and one that I found that is working.

The one the is working creates the toast object and declares the text at the same time.

Toast toast = 
Toast.makeText(QuizActivity.this,R.string.incorrect_toast, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

The one I made creates the object in the context, sets the text then the gravity and tries to show it.

Toast toast = new Toast(QuizActivity.this);
toast.makeText(QuizActivity.this,
               R.string.correct_toast,
               Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

Why is this crashing my app?

Elias
  • 2,602
  • 5
  • 28
  • 57

5 Answers5

1

you can try this

Toast toast = Toast.makeText(QuizActivity.this,"your string", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
1

Your

Toast toast = new Toast(QuizActivity.this);

constructs an empty Toast object. You must call setView(View) before you can call show().

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
            (ViewGroup) findViewById(R.id.custom_toast_container));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

as it stated in Android docs.

isabsent
  • 3,683
  • 3
  • 25
  • 46
  • Ah ok. So I have to go into the XML create a special container. Make an object out of that and "paint" to it with the toast if I don't just initialize it by setting the text from the get go? that seems a bit convoluted. – Elias May 26 '17 at 04:30
  • Roughly speaking - yes. And mark my answer as a correct :). – isabsent May 26 '17 at 05:59
1

You can create a toast object like this.

Toast toast = new Toast(QuizActivity.this);

This line of code construct an empty Toast object.

Then you have to call the setView() , setDuration() on your own. makeText() method is a static method. So, you can't call it from a toast object.

You can initialize a Toast object by calling the method makeText() method.

For this reason, the first code is working properly and the second is not.

Avijit Karmakar
  • 8,890
  • 6
  • 44
  • 59
0

 This is crashing because you are not called setView() method before calling show mehod.

 Calling constructor(Toast toast = new Toast(QuizActivity.this);) will construct an empty Toast object.This object will not contain a default view to show,So you have to call setView() method to set a view for the toast.Then only this will work.

Jinesh Francis
  • 3,377
  • 3
  • 22
  • 37
0

Most probably you are getting RuntimeException.

'java.lang.RuntimeException: setView must have been called'

You are just creating an instance of Toast uisng "new Toast(QuizActivity.this)" and you did not set any view for it. You have to set your own custom layout to Toast.

SOLUTION:

LayoutInflater inflate = (LayoutInflater)
        this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// Custom layout
View view = inflate.inflate(R.layout.custom_toast, null);
TextView tv = (TextView) view.findViewById(R.id.textView);
tv.setText("Hello world!");

// Toast
Toast toast = new Toast(QuizActivity.this);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);

// Set the Toast custom layout
toast.setView(view);
toast.show();

Here is custom_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:layout_margin="5px"
    android:padding="10dp"
    android:layout_gravity="center_horizontal">

    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/imageView"
            android:src="@mipmap/ic_launcher"
            android:layout_width="50dp" 
            android:layout_height="50dp"/>

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:text="Android Tutorial Custom Toast"
            android:textColor="#fff"
            android:textSize="16dp" 
            android:layout_gravity="center_vertical"/>

    </LinearLayout>
</LinearLayout>

Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61