通过父片段的公共方法更新片段

时间:2014-11-12 13:17:07

标签: java android android-fragments android-tabhost

我试图将数据发送到子片段,但我一直在从logCat中获取此信息:

W/System.err﹕ java.lang.NullPointerException
    W/System.err﹕ at com.myapp.fragments.CommunityClipsFragment.setNewData(CommunityClipsFragment.java:39)
    11-12 14:07:58.159  17977-17977/com.myappW/System.err﹕ at com.myapp.fragments.CommunitySearchFragment.onAPIResponse(CommunitySearchFragment.java:123)
    11-12 14:07:58.159  17977-17977/com.myapp W/System.err﹕ at com.myapp.server.Server$ServerTask.onPostExecute(Server.java:735)
    11-12 14:07:58.159  17977-17977/com.myapp W/System.err﹕ at com.myapp.server.Server$ServerTask.onPostExecute(Server.java:526)
    11-12 14:07:58.159  17977-17977/com.myapp W/System.err﹕ at android.os.AsyncTask.finish(AsyncTask.java:602)
    11-12 14:07:58.159  17977-17977/com.myapp W/System.err﹕ at android.os.AsyncTask.access$600(AsyncTask.java:156)
    11-12 14:07:58.159  17977-17977/com.myapp W/System.err﹕ at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
    11-12 14:07:58.159  17977-17977/com.myapp W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
    11-12 14:07:58.159  17977-17977/com.myapp W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
    11-12 14:07:58.159  17977-17977/com.myapp W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:4441)
    11-12 14:07:58.159  17977-17977/com.myapp W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
    11-12 14:07:58.159  17977-17977/com.myapp W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
    11-12 14:07:58.159  17977-17977/com.myapp W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    11-12 14:07:58.159  17977-17977/com.myapp W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    11-12 14:07:58.159  17977-17977/com.myapp W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

我想做什么: 在父片段中我有一个tabhost 2选项卡,但我使用asynctask和onpostExecute加载数据我从子片段调用setNewData方法。

这是父片段类:

public class CommunitySearchFragment extends Fragment {

    private FragmentTabHost mTabHost;
    private ArrayList<ClipboardVO> cArray;
    private ArrayList<ClipsVO> clipsArray;
    private int userId;
    private Bundle b;
    CommunityClipsFragment communityClipsFragment;
    FragmentSearchMyClipboards fragmentSearchMyClipboards;

    private void doTheRequest() {
        if (getArguments().getString(IntentConstants.KEYWORD) != null)
            if (getArguments().getString(IntentConstants.KEYWORD).length() > 0) {
                HashMap<String, String> params = new HashMap<String, String>();
                setUserData(params);
                params.put(Server.FEEDPARAMS_KEYWORD, getArguments().getString(IntentConstants.KEYWORD));
                params.put(Server.FEEDPARAMS_RESULTCOUNT, "100");
                placeRequest(Server.APIMETHOD_SEARCHCOMMUNITY, Json.METHODNAME_PARSESEARCHCOMMUNITY, params);
            }
    }

    @Override
    public void onAPIResponse(ResultVO result) {
        super.onAPIResponse(result);
        if (result.getApiMethod().equals(Server.APIMETHOD_SEARCHCOMMUNITY)) {

            HashMap<Integer, Object> searchList = (HashMap<Integer, Object>) result.getData();
            clipsArray = (ArrayList<ClipsVO>) searchList.get(Consts.CLIPS);
            ArrayList<Object> rClipsArray = new ArrayList<Object>();

            userId = Integer.parseInt(UserProfile.getinstance().getUserData().getUserId());
            if (clipsArray.size() > 0) {

                for (int i = 0; i < clipsArray.size(); i++) {
                    ClipsVO clip = clipsArray.get(i);
                    if (clip.getOwnerUserId().equals(userId + "")) {
                        if (rClipsArray.size() == 0) {
                            //Add header for the Current User's clips in the result
                            ClipsVO headerClip = new ClipsVO();
                            headerClip.setHeader(true);
                            headerClip.setDescription(getLocalizationString("lblCommunityMyClips"));
                            rClipsArray.add(headerClip);
                        }
                        rClipsArray.add(clipsArray.remove(i));
                        i--;
                    }
                }
                if (clipsArray.size() > 0) {
                    //Add header for the other Clipboards in the result
                    ClipsVO headerClip = new ClipsVO();
                    headerClip.setHeader(true);
                    headerClip.setDescription(getLocalizationString("lblClips"));
                    rClipsArray.add(headerClip);
                    rClipsArray.addAll(clipsArray);
                }

            }

            //Parse the Clipboards ffrom the result and group the user's clipboards seperately from others
            int userId = Integer.parseInt(UserProfile.getinstance().getUserData().getUserId());

            cArray = (ArrayList<ClipboardVO>) searchList.get(Consts.CLIPBOARD);
            ArrayList<Object> rArray = new ArrayList<Object>();
            if (cArray.size() > 0) {

                for (int i = 0; i < cArray.size(); i++) {
                    ClipboardVO clipboard = cArray.get(i);
                    if (clipboard.getUserId() == userId) {
                        if (rArray.size() == 0) {
                            //Add header for the Current User's Clipboards in the result
                            ClipboardVO headerClipboard = new ClipboardVO();
                            headerClipboard.setHeader(true);
                            headerClipboard.setClipboardName(getLocalizationString("lblMyClipboards"));
                            rArray.add(headerClipboard);
                        }
                        rArray.add(cArray.remove(i));
                        i--;
                    }
                }
                if (cArray.size() > 0) {
                    //Add header for the other Clipboards in the result
                    ClipboardVO headerClipboard = new ClipboardVO();
                    headerClipboard.setHeader(true);
                    headerClipboard.setClipboardName(getLocalizationString("lblClipboards"));
                    rArray.add(headerClipboard);
                    rArray.addAll(cArray);
                }
            }

            try {
                b.putSerializable("communityClipboards", rArray);
                b.putParcelableArrayList("communityClips", clipsArray);
                communityClipsFragment.setNewData(clipsArray);
                fragmentSearchMyClipboards.setNewData(rArray);
                Log.v("---", "putting comClips " + rClipsArray.size());
            } catch (Exception e) {
                e.printStackTrace();
            }
            searchList.remove(Consts.CLIPBOARD);
            searchList.remove(Consts.CLIPS);
            rClipsArray.addAll(rArray);
            searchList.put(Consts.COMMUNITY, rClipsArray);

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        doTheRequest();
        mView = inflater.inflate(R.layout.fragment_community_search_tab, container, false);

        communityClipsFragment= new CommunityClipsFragment();
        fragmentSearchMyClipboards=new FragmentSearchMyClipboards();

        mTabHost = (FragmentTabHost) mView.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.fragment_comminity);
        //setup tabhost
        Log.v("---", "adding tabs");
        b = new Bundle();
        mTabHost.addTab(mTabHost.newTabSpec("clips").setIndicator(getLocalizationString("lblClips")),
                communityClipsFragment.getClass(), b);

        mTabHost.addTab(mTabHost.newTabSpec("clipboards").setIndicator(getLocalizationString("lblClipboards")),
                fragmentSearchMyClipboards.getClass(), b);

        final int reverseTabs[] = {1, 0};
        mTabHost.getTabWidget().setDividerDrawable(R.color.color_translucent_grey);
        View v = mTabHost.getTabWidget().getChildTabViewAt(reverseTabs[mTabHost.getCurrentTab()]);
        ((TextView) v.findViewById(android.R.id.title)).setTypeface(null, Typeface.NORMAL);
        ((TextView) v.findViewById(android.R.id.title)).setTextColor(Color.parseColor("#a3a3a3"));
        v.setBackgroundResource(R.drawable.border);
        View v2 = mTabHost.getTabWidget().getChildTabViewAt(mTabHost.getCurrentTab());
        v2.setBackgroundColor(getResources().getColor(R.color.transparent));
        ((TextView) v2.findViewById(android.R.id.title)).setTextColor(getResources().getColor(R.color.color_orange));
        v2.setBackgroundResource(R.drawable.border);

        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                View v = mTabHost.getTabWidget().getChildTabViewAt(reverseTabs[mTabHost.getCurrentTab()]);
                ((TextView) v.findViewById(android.R.id.title)).setTypeface(null, Typeface.NORMAL);
                ((TextView) v.findViewById(android.R.id.title)).setTextColor(Color.parseColor("#a3a3a3"));
                v.setBackgroundResource(R.drawable.border);
                View v2 = mTabHost.getTabWidget().getChildTabViewAt(mTabHost.getCurrentTab());
                v2.setBackgroundResource(R.drawable.border);
                ((TextView) v2.findViewById(android.R.id.title)).setTypeface(null, Typeface.BOLD);
                ((TextView) v2.findViewById(android.R.id.title)).setTextColor(getResources().getColor(R.color.color_orange));
            }
        });


        return mView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mTabHost = null;
    }
}

和一个子片段:

public class CommunityClipsFragment extends Fragment {

    private View mView;
    private ListView list;

    private MyClipboardSearchAdapter adapter;
    private ArrayList<ClipsVO> rClipsArray;

    public void setNewData(ArrayList<ClipsVO> data){
        if(data==null){
            data=new ArrayList<ClipsVO>();
        }
        rClipsArray=data;
        //CommunityClipsFragment.java line:39
        adapter.notifyDataSetChanged();
    }

    private void setupData() {

        adapter = new MyClipboardSearchAdapter(getActivity(),rClipsArray);
        list.setAdapter(adapter);
        TextView emptyTxt = (TextView) mView.findViewById(R.id.empty);
        emptyTxt.setText(getLocalizationString("txtPeopleSearchNoResult"));
        emptyTxt.setTextSize(14);
        list.setEmptyView(emptyTxt);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Object itemObject = rClipsArray.get(position);
                ArrayList<ClipsVO> clipsList = new ArrayList<ClipsVO>();
                ClipsVO clipVO = (ClipsVO) itemObject;
                clipsList.add(clipVO);
                Intent intent = new Intent(getActivity(), ClipsListDetailActivity.class);

                intent.putExtra(IntentConstants.CLIP_DATA, clipVO);
                intent.putExtra(IntentConstants.CLIP_ID, clipVO.getClipId());
                intent.putExtra(IntentConstants.IS_USERCLIPS, false);

                intent.putExtra(IntentConstants.IS_COMMUNITYCLIPS, true);
                VisitorData.getinstance().setClipsList(rClipsArray);

                intent.putExtra(IntentConstants.CLIP_INDEX_POSITION, 0);
                intent.putExtra(IntentConstants.CLIPBOARD_INDEX_POSITION, 0);
                startActivity(intent);
            }
        });


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_list_layout, container, false);
        list = (ListView) mView.findViewById(R.id.fragment_list_layout_list);

        rClipsArray = new ArrayList<ClipsVO>();

        if (getArguments().getParcelableArrayList("communityClips") != null) {
            Log.v("---", "community clips size:" + (getArguments().getParcelableArrayList("communityClips")).size());
            rClipsArray = new ArrayList<ClipsVO>();
            rClipsArray = getArguments().getParcelableArrayList("communityClips");

            setupData();
        }
        return mView;
    }
}

所以我的问题是我做错了什么。如何从子片段的父片段发送新数据,并显示新数据?

P.S。很抱歉有很多代码,但我试着尽可能地解释

0 个答案:

没有答案