Reputation: 1032
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.
Upvotes: 1
Views: 419
Reputation: 1932
You problem is that you have this line
inflater = getActivity().getLayoutInflater();
in the onCreateView. It needs to be called after the onAttach(). SO do it in the onStart or in onActivityCreated.
Upvotes: 1
Reputation: 1032
I had posted just onCreate(..) method and the method where Toast is inflated.
public class POKFragment extends ListFragment {
ListView list;
View rootView, view;
LayoutInflater inflater;
ArrayAdapter<String> courseAdapter = null;
DatabaseHandler db = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_pok, container, false);
list = (ListView) rootView.findViewById(android.R.id.list);
inflater = getActivity().getLayoutInflater();
int[] colors = { 0, 0xFF0072BC, 0 };
list.setDivider(new GradientDrawable(Orientation.LEFT_RIGHT, colors));
list.setDividerHeight(3);
courseAdapter = new ArrayAdapter<String>(this.getActivity(),
android.R.layout.simple_list_item_1, courses);
setListAdapter(courseAdapter);
courseAdapter.notifyDataSetChanged();
return rootView;
}
...............
...............
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, null);
Toast toast = new Toast(getActivity());
toast.setGravity(Gravity.TOP, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(view);
toast.show();
}
}
...........
...........
}
Upvotes: 1
Reputation: 7439
If your xml is inflated in your Fragment itself then no need to give any Activity parameter there.
view = inflater.inflate(R.layout.custom_toast_layout_course_deleted,null);
Upvotes: 0
Reputation: 18933
Need to change
view = inflater.inflate(
R.layout.custom_toast_layout_course_deleted,
null));
Also check that your inflater
must not be null.
Upvotes: 1