片段之间的Android通信

时间:2015-01-22 21:00:35

标签: android android-fragments

http://pastebin.com/p9FCCMX1I尝试通过活动将数据从一个片段发送到另一个片段。 可以将数据从GroupFragment发送到ChatActivity(id包含正确的值)但是当它到达时:

@Override
public void respond(String id) {
    interfaceDataCommunicatorFromActivity.respond(id);
}

在我的ChatActivity中,它会抛出一个NUllPointException,我不知道如何让它工作?

(当我只粘贴部分代码时,我试着让你更容易阅读,如果它没有用,我会很友好并纠正我!对于所有代码:“http://pastebin.com/p9FCCMX1”)

ChatActivity:

public class ChatActivity extends Activity implements
    GroupFragment.OnFragmentInteractionListener,
    //ChatFragment.OnFragmentInteractionListener,
    GroupFragment.InterfaceDataCommunicator {

    public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity;

    @Override
    public void respond(String id) {
        interfaceDataCommunicatorFromActivity.respond(id);
    }

    public interface InterfaceDataCommunicatorFromActivity {
        public void respond(String id);
    }
}

GroupFragment:

public class GroupFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    public InterfaceDataCommunicator interfaceDataCommunicater;
    ...../


    public void CreateNewGroup(String groupName) {

        Map<String, Group> newGroup = new HashMap<>();

        Firebase  firebaserootRef = new Firebase("https://luminous-heat-420.firebaseio.com");

        Firebase firebaseGroup = firebaserootRef.child("").push();

        Group group = new Group();

        group.SetID(firebaseGroup.getKey());
        group.SetName(groupName);

        String id = firebaseGroup.getKey();
        firebaseGroup.child("id").setValue(group.GetId());
        firebaseGroup.child("name").setValue(group.GetName());

        newGroup.put(id, group);

        interfaceDataCommunicater.respond(group.GetId());
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }

        try {
            interfaceDataCommunicater = (InterfaceDataCommunicator) activity;
        } catch (ClassCastException c) {
            throw new ClassCastException(activity.toString()
                    + " must implement InterfaceDataCommunicator");
        }
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

    public interface InterfaceDataCommunicator{
        public void respond(String id);
    }
}

ChatFragment:

public class ChatFragment extends Fragment implements
    View.OnClickListener,
    ChatActivity.InterfaceDataCommunicatorFromActivity {

    public ChatActivity.InterfaceDataCommunicatorFromActivity
           interfaceDataCommunicatorFromActivity; 

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }

        try {
            interfaceDataCommunicatorFromActivity = (ChatActivity
               .InterfaceDataCommunicatorFromActivity) activity;    
        } catch (ClassCastException c) {
            throw new ClassCastException(activity.toString()
                    + " must implement InterfaceDataCommunicator");
        }
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

    @Override
    public void respond(String id) {
        this.groupID = id;
    }
}

1 个答案:

答案 0 :(得分:0)

您活动中的interfaceDataComunicatorFromActivity无用且您获得了NPE,因为它没有引用它。如果您只想在ChatFragment中找到论坛的ID,只需通过setter中的ChangeToChatFragment()传递:

public void ChangeToChatFragment(String groupId) {
    ChatFragment groupFragment = ChatFragment.newInstance("", "");
    groupFragment.setGroupId(groupId);
    FragmentManager fM = getFragmentManager();
    FragmentTransaction fT = fM.beginTransaction();
    fT.replace(R.id.container_chat, groupFragment, null);
    fT.addToBackStack("go to chat fragmement");
    fT.commit();
}

修改

ChatFragment添加:

public void setGroupId(String groupId){
   this.groupId = groupId;
}

然后将groupId作为参数传递给ChangeToChatFragment