I didn't find any post to display customized toast in ListFragment. I have search all around. Here is my customized xml file used to display a toast.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CC000000" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/course_deleted"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white" >
</TextView>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_margin="5dip"
android:contentDescription="@string/delete"
android:src="@drawable/ic_action_discard" >
</ImageView>
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:gravity="center"
android:textColor="@android:color/white" >
</TextView>
</RelativeLayout>
and a method that inflate this xml is:
private void delete(String course) {
DatabaseHandler db = new DatabaseHandler(this.getActivity());
boolean status = db.deleteCourse(UNI, course);
if (status) {
view = inflater.inflate(
R.layout.custom_toast_layout_course_deleted,
(ViewGroup) getActivity()
.findViewById(R.id.relativeLayout5));
Toast toast = new Toast(getActivity());
toast.setGravity(Gravity.TOP, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
toast.show();
}
}
But when my app runs it show this error:
11-13 09:48:13.521: E/AndroidRuntime(11052): FATAL EXCEPTION: main
11-13 09:48:13.521: E/AndroidRuntime(11052): java.lang.NullPointerException
11-13 09:48:13.521: E/AndroidRuntime(11052): at ......delete(POKFragment.java:173)
11-13 09:48:13.521: E/AndroidRuntime(11052): at ......access$0(POKFragment.java:161)
11-13 09:48:13.521: E/AndroidRuntime(11052): at ......onClick(POKFragment.java:107)
and Line 173 is:
view = inflater.inflate(R.layout.custom_toast_layout_course_deleted,(ViewGroup) getActivityity().findViewById(R.id.relativeLayout5));
What is wrong in this code? It works fine for Activity but goes wrong with ListFragment.
Need to change
Also check that your
inflater
must not be null.