我正在使用DialogFragment中的ExpandableListView添加为过滤器。它看起来应该是Filter Fragment。
现在要实现这个,我做了类似的事情。
public class FilterDialogForCharity extends DialogFragment {
MyExpandableListAdapter expandableListAdapter;
ExpandableListView listView;
ArrayList<String> titles;
HashMap<String,ArrayList<String>> group;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
titles = new ArrayList<>();
group = new HashMap<String,ArrayList<String>>();
initialiseList();
View view = inflater.inflate(R.layout.filter_dialog,container,false);
listView = (ExpandableListView) view.findViewById(R.id.expandable_filter_list);
expandableListAdapter = new MyExpandableListAdapter(titles,getActivity(),group);
listView.setAdapter(expandableListAdapter);
return view;
}
private void initialiseList() {
titles.add("Work");
titles.add("Region");
titles.add("Rating");
// Adding child data
ArrayList<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
ArrayList<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");
ArrayList<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");
group.put(titles.get(0), top250); // Header, Child data
group.put(titles.get(1), nowShowing);
group.put(titles.get(2), comingSoon);
expandableListAdapter.notifyDataSetChanged();
}
} }
MyExpandableListAdapter.java
public class FilterDialogForCharity extends DialogFragment {
MyExpandableListAdapter expandableListAdapter;
ExpandableListView listView;
ArrayList<String> titles;
HashMap<String,ArrayList<String>> group;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
titles = new ArrayList<>();
group = new HashMap<>();
View view = inflater.inflate(R.layout.filter_dialog,container,false);
listView = (ExpandableListView) view.findViewById(R.id.expandable_filter_list);
expandableListAdapter = new MyExpandableListAdapter(titles,getActivity(),group);
initialiseList();
listView.setAdapter(expandableListAdapter);
return view;
}
private void initialiseList() {
titles.add("Work");
titles.add("Region");
titles.add("Rating");
// Adding child data
ArrayList<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
ArrayList<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");
ArrayList<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");
group.put(titles.get(0), top250); // Header, Child data
group.put(titles.get(1), nowShowing);
group.put(titles.get(2), comingSoon);
expandableListAdapter.notifyDataSetChanged();
}
} StackTrace日志
03-25 02:19:28.609 17728-17728/healthandcharity.socialcause.care E/MotionRecognitionManager: mSContextService = null
03-25 02:19:28.609 17728-17728/healthandcharity.socialcause.care E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@c29c17d
03-25 02:19:28.649 17728-17728/healthandcharity.socialcause.care E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at healthandcharity.socialcause.care.ExpandableListAdapter.getGroupView(ExpandableListAdapter.java:73)
at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
at android.widget.AbsListView.obtainView(AbsListView.java:2934)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1305)
at android.widget.ListView.onMeasure(ListView.java:1212)
at android.view.View.measure(View.java:20182)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:20182)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20182)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20182)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3094)
at android.view.View.measure(View.java:20182)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2643)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1573)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1890)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1486)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7467)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
at android.view.Choreographer.doCallbacks(Choreographer.java:695)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
03-25 02:19:28.869 17728-17728/healthandcharity.socialcause.care E/AndroidRuntime: FATAL EXCEPTION: main
Process: healthandcharity.socialcause.care, PID: 17728
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at healthandcharity.socialcause.care.ExpandableListAdapter.getGroupView(ExpandableListAdapter.java:73)
at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
at android.widget.AbsListView.obtainView(AbsListView.java:2934)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1305)
at android.widget.ListView.onMeasure(ListView.java:1212)
at android.view.View.measure(View.java:20182)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:20182)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20182)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20182)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3094)
at android.view.View.measure(View.java:20182)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2643)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1573)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1890)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1486)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7467)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
at android.view.Choreographer.doCallbacks(Choreographer.java:695)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)