jzk15666378163 2016-05-12 14:41 采纳率: 0%
浏览 1395

代码有错误,求大神帮忙指点迷津!!!!

public class ChatFragment extends BaseFragment implements OnClickListener,ReceiveMsgListener{

// private NetThreadHelper netThreadHelper;

// private ImageView chat_item_head; //头像
private TextView chat_name; //名字及IP
private TextView chat_mood; //组名
private Button chat_quit; //退出按钮
private ListView chat_list; //聊天列表
private EditText chat_input; //聊天输入框
private Button chat_send; //发送按钮

private List<ChatMessage> msgList;  //用于显示的消息list
private String receiverName;            //要接收本activity所发送的消息的用户名字
private String receiverIp;          //要接收本activity所发送的消息的用户IP
private String receiverGroup;           //要接收本activity所发送的消息的用户组名
private ChatListAdapter adapter;    //ListView对应的adapter
private String selfName;
private String selfGroup;

private final static int MENU_ITEM_SENDFILE = Menu.FIRST;   //发送文件
private final static int MENU_ITEM_EXIT = Menu.FIRST + 1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View chatLayout = inflater.inflate(R.layout.chat, container,
            false);
    return chatLayout;

// findViews();

// netThreadHelper = NetThreadHelper.newInstance();
msgList = new ArrayList();
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
receiverName = bundle.getString("receiverName");
receiverIp = bundle.getString("receiverIp");
receiverGroup = bundle.getString("receiverGroup");
selfName = "android飞鸽";
selfGroup = "android";

    chat_name.setText(receiverName + "(" + receiverIp + ")");
    chat_mood.setText("组名:" + receiverGroup);
    chat_quit.setOnClickListener(this);
    chat_send.setOnClickListener(this);

    Iterator<ChatMessage> it = netThreadHelper.getReceiveMsgQueue().iterator();
    while(it.hasNext()){    //循环消息队列,获取队列中与本聊天activity相关信息
        ChatMessage temp = it.next();
        //若消息队列中的发送者与本activity的消息接收者IP相同,则将这个消息拿出,添加到本activity要显示的消息list中
        if(receiverIp.equals(temp.getSenderIp())){ 
            msgList.add(temp);  //添加到显示list
            it.remove();        //将本消息从消息队列中移除
        }
    }

    adapter = new ChatListAdapter(getActivity(), msgList);
    chat_list.setAdapter(adapter);

    netThreadHelper.addReceiveMsgListener(this);    //注册到listeners
}

public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);
    chat_name = (TextView)view.findViewById(R.id.chat_name);
    chat_mood = (TextView) view.findViewById(R.id.chat_mood);
    chat_quit = (Button) view.findViewById(R.id.chat_quit);
    chat_list = (ListView) view.findViewById(R.id.chat_list);
    chat_input = (EditText) view.findViewById(R.id.chat_input);
    chat_send = (Button) view.findViewById(R.id.chat_send);
}

@Override
public void processMessage(Message msg) {
    // TODO Auto-generated method stub
    switch(msg.what){
    case IpMessageConst.IPMSG_SENDMSG:
        adapter.notifyDataSetChanged(); //刷新ListView
        break;

    case IpMessageConst.IPMSG_RELEASEFILES:{ //拒绝接受文件,停止发送文件线程
        if(NetTcpFileSendThread.server != null){
            try {
                NetTcpFileSendThread.server.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
        break;

    case UsedConst.FILESENDSUCCESS:{    //文件发送成功
        makeTextShort("文件发送成功");
    }
        break;


    }   //end of switch
}

@Override
public boolean receive(ChatMessage msg) {
    // TODO Auto-generated method stub
    if(receiverIp.equals(msg.getSenderIp())){   //若消息与本activity有关,则接收
        msgList.add(msg);   //将此消息添加到显示list中
        sendEmptyMessage(IpMessageConst.IPMSG_SENDMSG); //使用handle通知,来更新UI
        BaseFragment.playMsg();
        return true;
    }

    return false;
}

@Override
public void finish() {
    // TODO Auto-generated method stub
    //一定要移除,不然信息接收会出现问题
    netThreadHelper.removeReceiveMsgListener(this);
    super.finish();
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v == chat_send){
        sendAndAddMessage();    
    }else if(v == chat_quit){
        finish();
    }
}

/**
 * 发送消息并将该消息添加到UI显示
 */
private void sendAndAddMessage(){
    String msgStr = chat_input.getText().toString().trim();
    if(!"".equals(msgStr)){
        //发送消息
        IpMessageProtocol sendMsg = new IpMessageProtocol();
        sendMsg.setVersion(String.valueOf(IpMessageConst.VERSION));
        sendMsg.setSenderName(selfName);
        sendMsg.setSenderHost(selfGroup);
        sendMsg.setCommandNo(IpMessageConst.IPMSG_SENDMSG);
        sendMsg.setAdditionalSection(msgStr);
        InetAddress sendto = null;
        try {
            sendto = InetAddress.getByName(receiverIp);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            Log.e("MyFeiGeChatActivity", "发送地址有误");
        }
        if(sendto != null)
            netThreadHelper.sendUdpData(sendMsg.getProtocolString() + "\0", sendto, IpMessageConst.PORT);

        //添加消息到显示list
        ChatMessage selfMsg = new ChatMessage("localhost", selfName, msgStr, new Date());
        selfMsg.setSelfMsg(true);   //设置为自身消息
        msgList.add(selfMsg);   

    }else{
        makeTextShort("不能发送空内容");
    }

    chat_input.setText("");
    adapter.notifyDataSetChanged();//更新UI
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    super.onCreateOptionsMenu(menu);
    menu.add(0, MENU_ITEM_SENDFILE, 0, "发送文件");
    menu.add(0, MENU_ITEM_EXIT, 0, "退出");

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch(item.getItemId()){
    case MENU_ITEM_SENDFILE:
        Intent intent = new Intent(getActivity(), FileFragment.class);
        startActivityForResult(intent, 0);

        break;
    case MENU_ITEM_EXIT:
        finish();
        break;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
        //得到发送文件的路径
        Bundle bundle = data.getExtras();

        String filePaths = bundle.getString("filePaths");   //附加文件信息串,多个文件使用"\0"进行分隔

// Toast.makeText(this, filePaths, Toast.LENGTH_SHORT).show();

        String[] filePathArray = filePaths.split("\0");


        //发送传送文件UDP数据报
        IpMessageProtocol sendPro = new IpMessageProtocol();
        sendPro.setVersion("" +IpMessageConst.VERSION);
        sendPro.setCommandNo(IpMessageConst.IPMSG_SENDMSG | IpMessageConst.IPMSG_FILEATTACHOPT);
        sendPro.setSenderName(selfName);
        sendPro.setSenderHost(selfGroup);
        String msgStr = ""; //发送的消息

        StringBuffer additionInfoSb = new StringBuffer();   //用于组合附加文件格式的sb
        for(String path:filePathArray){
            File file = new File(path);
            additionInfoSb.append("0:");
            additionInfoSb.append(file.getName() + ":");
            additionInfoSb.append(Long.toHexString(file.length()) + ":");       //文件大小十六进制表示
            additionInfoSb.append(Long.toHexString(file.lastModified()) + ":"); //文件创建时间,现在暂时已最后修改时间替代
            additionInfoSb.append(IpMessageConst.IPMSG_FILE_REGULAR + ":");
            byte[] bt = {0x07};     //用于分隔多个发送文件的字符
            String splitStr = new String(bt);
            additionInfoSb.append(splitStr);
        }

        sendPro.setAdditionalSection(msgStr + "\0" + additionInfoSb.toString() + "\0");

        InetAddress sendto = null;
        try {
            sendto = InetAddress.getByName(receiverIp);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            Log.e("MyFeiGeChatActivity", "发送地址有误");
        }
        if(sendto != null)
            netThreadHelper.sendUdpData(sendPro.getProtocolString(), sendto, IpMessageConst.PORT);

        //监听2425端口,准备接受TCP连接请求
        Thread netTcpFileSendThread = new Thread(new NetTcpFileSendThread(filePathArray));
        netTcpFileSendThread.start();   //启动线程
    }
}

}
![![图片说明](https://img-ask.csdn.net/upload/201605/12/1463064059_713861.png)图片说明](https://img-ask.csdn.net/upload/201605/12/1463063989_240367.png)图片说明
求大神指点

  • 写回答

3条回答 默认 最新

  • jzk15666378163 2016-05-12 14:41
    关注

    图片说明

    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面