当Activity被销毁时,Fragment Listener不起作用

时间:2016-03-24 23:30:07

标签: java android sockets android-fragments listener

我使用android和socket进行聊天应用程序。聊天界面在片段内。虽然两个设备都显示聊天界面,但运行良好。但当其中一个设备关闭时,它会给我错误的力量关闭。

这是我的代码。 MainActivity.java

public class ChatFragment extends Fragment {
    private ListView listchat;
    private Button send;
    private EditText msgText;
    public ChatAdapter adapter;
    private ArrayList<ChatModel> items = new ArrayList<ChatModel>();
    private boolean isactive;
    private Toolbar chatToolbar;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private Socket socket;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        View v;
        v = inflater.inflate(R.layout.chat_fragment, container, false);

        SocketSingleton singleton = (SocketSingleton) getActivity().getApplication();
        socket = singleton.getmSocket();

        // this is listener for message coming 
        socket.on("message", messageListener);

        msgText = (EditText) v.findViewById(R.id.editMsg);

        adapter = new ChatAdapter(getActivity(), R.layout.chat_list, items);
        listchat = (ListView) v.findViewById(R.id.listchat);
        listchat.setAdapter(adapter);
        listchat.setDivider(null);

        send = (Button) v.findViewById(R.id.btnSend);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addMessage();
            }
        });


        return v;
    }

    private void addMessage() {
        ChatModel item = new ChatModel();
        item.setMsg(msgText.getText().toString());
        items.add(item);
        adapter.notifyDataSetChanged();
        String message = msgText.getText().toString();
        socket.emit("message", message);
        msgText.setText("");
    }

    private Emitter.Listener messageListener = new Emitter.Listener() {
        @Override
        public void call(final Object... args) {
            // error start from here
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    JSONObject json = (JSONObject) args[0];
                    String msg;

                    try {
                        msg = json.getString("message").toString();
                        ChatModel model = new ChatModel();
                        model.setMsg(msg);
                        items.add(model);
                        adapter.notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
        }
    };

}

和ChatFragment.java

FATAL EXCEPTION: EventThread
java.lang.NullPointerException

在longcat中它说。

@Component({
    selector: 'my-app',
    templateUrl:'/partials/index',

在应用程序中,当消息来临和活动关闭时强制关闭。如何在消息到来时处理此侦听器,并且活动关闭?感谢。

2 个答案:

答案 0 :(得分:0)

您必须在片段的messageListener中将null设置为onDetach()

@Override
    public void onDetach() {
        super.onDetach();
        messageListener = null;
    }

答案 1 :(得分:0)

您应首先检查getActivity()

if( getActivity() != null ){
getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    JSONObject json = (JSONObject) args[0];
                    String msg;

                    try {
                        msg = json.getString("message").toString();
                        ChatModel model = new ChatModel();
                        model.setMsg(msg);
                        items.add(model);
                        adapter.notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
}