You are using a view that is already attached to another view hierarchy. You have to inflate your custom toast view separately like this:
custom_toast.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:padding="5dp">
<TextView
android:id="@+id/tvToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
And that's how you use it:
LayoutInflater inflater = LayoutInflater.from(context);
View customToastView = inflater.inflate(R.layout.custom_toast, null);
TextView tvToast = (TextView) customToastView.findViewById(R.id.tvToast);
tvToast.setText("This is a custom toast with centered text");
Toast toast = new Toast(context);
toast.setView(customToastView);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();