Frage

I want to be able to call a Utills class method to pop custom Toast messages on screen.

My code:

static public void ShowToast(Context context,String message, int duration)
 {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

    // Inflate the Layout
    View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.custom_toast_layout));

    TextView text = (TextView) layout.findViewById(R.id.textToShow);
    // Set the Text to show in TextView
    text.setText(message);

    Toast toast = new Toast(context);
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(duration);
    toast.setView(layout);
    toast.show();
 }

I have a proble with line 4 where i call

(ViewGroup) findViewById(R.id.custom_toast_layout)

I have a compilation error which states:

The method findViewById(int) is undefined for the type SystemUtills

How can i fix this?

Thanks.

War es hilfreich?

Lösung

findViewById is a method of Activity, so in this line

View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.custom_toast_layout));

you need context.findViewById(R.id.custom_toast_layout)

That's the reason for that compile time error.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top