jzk15666378163
jzk15666378163
采纳率0%
2016-05-12 01:09 阅读 1.5k

把一个activity改成Fragment,遇到了错误,求大神指导!!

public void finish() {
// TODO Auto-generated method stub
super.finish();
queue.removeLast();
}
其中的finish有错误,求大神指点

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • shenshucong520 glustful 2016-05-12 01:12

    fragment有finish方法吗,你估计想调用getActivity().finish()吧

    点赞 评论 复制链接分享
  • GHSYue GHSYue 2016-05-12 03:21

    粘个完整的代码出来,,,

    点赞 评论 复制链接分享
  • jzk15666378163 jzk15666378163 2016-05-12 14:32

    package com.example.fragmentdemo;

    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Queue;

    import org.apache.http.conn.util.InetAddressUtils;

    import com.example.fragmentdemo.R;
    import com.transport.UserExpandableListAdapter;
    import com.transport.ChatMessage;
    import com.transport.User;
    import com.transport.IpMessageConst;

    import android.app.Fragment;
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.net.wifi.WifiInfo;
    import android.net.wifi.WifiManager;
    import android.os.Bundle;
    import android.os.Message;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ExpandableListView;
    import android.widget.TextView;
    import android.widget.Toast;

    public class ContactsFragment extends Fragment implements OnClickListener {

    public static String hostIp;
    
    private ExpandableListView userList;
    
    private UserExpandableListAdapter adapter;
    private List<String> strGroups; //所有一级菜单名称集合
    private List<List<User>> children;
    
    private TextView totalUser;
    private Button refreshButton;
    private TextView ipTextView;;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View contactsLayout = inflater.inflate(R.layout.contacts_layout,
                container, false);
    

    // return contactsLayout;

    if(!isWifiActive()){    //若wifi没有打开,提示
        Toast.makeText(getActivity(), R.string.no_wifi, Toast.LENGTH_LONG).show();
    }
    
    
    findViews();
    
    strGroups = new ArrayList<String>(); //所有一级菜单名称集合
    children = new ArrayList<List<User>>();
    

    // netThreadHelper = NetThreadHelper.newInstance();
    BaseFragment.netThreadHelper.connectSocket(); //开始监听数据
    BaseFragment.netThreadHelper.noticeOnline(); //广播上线

    adapter = new UserExpandableListAdapter(getActivity(), strGroups, children);
    userList.setAdapter(adapter);
    
    refreshButton.setOnClickListener(this);
    refreshViews();
    return contactsLayout;
    

    }

    @Override
    public void finish() {
    // TODO Auto-generated method stub
    super.finish();
    BaseFragment.netThreadHelper.noticeOffline(); //通知下线
    BaseFragment.netThreadHelper.disconnectSocket(); //停止监听

    }

    public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);
    totalUser =(TextView)view.findViewById(R.id.totalUser);
    userList = (ExpandableListView)view.findViewById(R.id.userlist);
    refreshButton = (Button) view.findViewById(R.id.refresh);
    ipTextView = (TextView) view.findViewById(R.id.mymood);
    hostIp = getLocalIpAddress();
    ipTextView.setText(hostIp); //设置IP
    }

    @Override
    public void processMessage(Message msg) {
    // TODO Auto-generated method stub
    switch(msg.what){
    case IpMessageConst.IPMSG_BR_ENTRY:
    case IpMessageConst.IPMSG_BR_EXIT:
    case IpMessageConst.IPMSG_ANSENTRY:
    case IpMessageConst.IPMSG_SENDMSG:
    refreshViews();
    break;
    }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if (keyCode == KeyEvent.KEYCODE_BACK)
    exit();
    return true;
    }

    //更新数据和UI显示
    private void refreshViews(){

    //清空数据
    strGroups.clear();
    children.clear();

    Map<String,User> currentUsers = new HashMap<String, User>();
    currentUsers.putAll(BaseFragment.netThreadHelper.getUsers());
    Queue<ChatMessage> msgQueue = BaseFragment.netThreadHelper.getReceiveMsgQueue();
    Map<String, Integer> ip2Msg = new HashMap<String, Integer>();   //IP地址与未收消息个数的map
    //遍历消息队列,填充ip2Msg
    Iterator<ChatMessage> it = msgQueue.iterator();
    while(it.hasNext()){
        ChatMessage chatMsg = it.next();
        String ip = chatMsg.getSenderIp();  //得到消息发送者IP
        Integer tempInt = ip2Msg.get(ip);
        if(tempInt == null){    //若map中没有IP对应的消息个数,则把IP添加进去,值为1
            ip2Msg.put(ip, 1);
        }else{  //若已经有对应ip,则将其值加一
            ip2Msg.put(ip, ip2Msg.get(ip)+1);
        }
    }
    
    //遍历currentUsers,更新strGroups和children
    Iterator<String> iterator = currentUsers.keySet().iterator();
    while (iterator.hasNext()) {
        User user = currentUsers.get(iterator.next());  
        //设置每个在线用户对应的未收消息个数
        if(ip2Msg.get(user.getIp()) == null){
            user.setMsgCount(0);
        }else{
            user.setMsgCount(ip2Msg.get(user.getIp()));
        }
    
        String groupName = user.getGroupName();
        int index = strGroups.indexOf(groupName);
        if(index == -1){ //没有相应分组,则添加分组,并添加对应child
            strGroups.add(groupName);
    

    // List> childData = new ArrayList>();
    // Map child = new HashMap();
    // child.put("userName", user.getUserName());
    // childData.add(child);
    // children.add(childData);

            List<User> childData = new ArrayList<User>();
            childData.add(user);
            children.add(childData);
        }else{  //已存在分组,则将对应child添加到相对应分组中
    

    // Map child = new HashMap();
    // child.put("userName", user.getUserName());
    // children.get(index).add(child);
    children.get(index).add(user);
    }

    }
    
    //更新groups
    

    // for(int i = 0; i < strGroups.size(); i++){
    // Map groupMap = new HashMap();
    // groupMap.put("group", strGroups.get(i));
    // groups.add(groupMap);
    // }

    adapter.notifyDataSetChanged(); //更新ExpandableListView
    
    String countStr = "当前在线" + currentUsers.size() +"个用户";
    totalUser.setText(countStr);    //更新TextView
    

    }

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.equals(refreshButton)){ //若是刷新
    BaseFragment.netThreadHelper.refreshUsers();
    refreshViews();
    }

    }

    //判断wifi是否打开
    public boolean isWifiActive(){
    ConnectivityManager mConnectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if(mConnectivity != null){
    NetworkInfo[] infos = mConnectivity.getAllNetworkInfo();

        if(infos != null){
            for(NetworkInfo ni: infos){
                if("WIFI".equals(ni.getTypeName()) && ni.isConnected())
                    return true;
            }
        }
    }
    
    return false;
    

    }

    //得到本机IP地址
    public String getLocalIpAddress(){
    try{
    Enumeration en = NetworkInterface.getNetworkInterfaces();
    while(en.hasMoreElements()){
    NetworkInterface nif = en.nextElement();
    Enumeration enumIpAddr = nif.getInetAddresses();
    while(enumIpAddr.hasMoreElements()){
    InetAddress mInetAddress = enumIpAddr.nextElement();
    if(!mInetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(mInetAddress.getHostAddress())){
    return mInetAddress.getHostAddress().toString();
    }
    }
    }
    }catch(SocketException ex){
    Log.e("MyFeiGeActivity", "获取本地IP地址失败");
    }

    return null;
    

    }

    //获取本机MAC地址
    public String getLocalMacAddress(){
    WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    return info.getMacAddress();
    }

    }
    这是完整的代码,求指点!!!!

    点赞 评论 复制链接分享

相关推荐