在片段中填充可扩展列表视图

时间:2014-05-01 18:01:20

标签: android android-fragments expandablelistview

我有一个活动,其中包含一个包含Expandable listview的片段。此列表是从Web服务填充的。当在此列表上按下子项时,该片段将替换为另一个片段。问题是当我单击后退按钮时,前一个片段从Web服务重新加载列表。我在列表中填写片段的onCreateView()。我知道这个论坛很糟糕,我把填充逻辑移到onActivityCreated(),但是当我尝试引用可扩展列表视图时,我得到NullPointerException。有人可以帮我确定问题吗?

这是我的代码

public class MainBodyArea extends Fragment {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<MainBodyAreas> listDataHeader;
ArrayList<MainBodyAreas> mainList = new ArrayList<MainBodyAreas>();
HashMap<MainBodyAreas, List<SubBodyAreas>> listDataChild;
Communicator communicator;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater
            .inflate(R.layout.main_body_areas, container, false);
    getActivity().setTitle("Body Areas");
    expListView = (ExpandableListView) view
            .findViewById(R.id.mainBodyExpandable);
    return view;
    }

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            int subBodyID = listDataChild
                    .get(listDataHeader.get(groupPosition))
                    .get(childPosition).getID();
            String subBodyName = listDataChild
                    .get(listDataHeader.get(groupPosition))
                    .get(childPosition).getName();
                Toast.makeText(getActivity(), "ID : " + subBodyID + "Name : " + subBodyName ,
                        Toast.LENGTH_SHORT).show();
                communicator.respond(subBodyName);
                return false;
            }
        });
    listDataHeader = new ArrayList<MainBodyAreas>();
    listDataChild = new HashMap<MainBodyAreas, List<SubBodyAreas>>();
    new AsyncLoadMainDetails().execute();

    listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader,
            listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);

    }
}

logcat的

05-01 22:50:51.885: E/AndroidRuntime(13058): FATAL EXCEPTION: main
05-01 22:50:51.885: E/AndroidRuntime(13058): Process: com.dreamcode.healttracker, PID: 13058
05-01 22:50:51.885: E/AndroidRuntime(13058): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dreamcode.healttracker/com.dreamcode.healttracker.SymptomNavigator}: java.lang.NullPointerException
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.os.Looper.loop(Looper.java:136)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.app.ActivityThread.main(ActivityThread.java:5017)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at java.lang.reflect.Method.invokeNative(Native Method)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at java.lang.reflect.Method.invoke(Method.java:515)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at dalvik.system.NativeStart.main(Native Method)
05-01 22:50:51.885: E/AndroidRuntime(13058): Caused by: java.lang.NullPointerException
05-01 22:50:51.885: E/AndroidRuntime(13058):    at com.dreamcode.healttracker.MainBodyArea.onCreate(MainBodyArea.java:107)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.app.Activity.performStart(Activity.java:5241)
05-01 22:50:51.885: E/AndroidRuntime(13058):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)

0 个答案:

没有答案