问题描述
- 代码有错误,求大神帮忙指导!!!!
-
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();
}
解决方案
以后问问题的时候,不要把一笸箩代码全部粘贴进来。别人都懒得看,最好的先把错误发出来。别人如果知道就直接回答了。如果不知道,你再把代码贴出来。
解决方案四:
java编程错误 求大神指导
求大神指点,谢谢啦
解决方案八:
鼠标放在有红线的地方,会有一个浮床显示错误信息
你这发上来的是“产生错误的代码”,完全没有任何“错误信息”
解决方案九:
都不说什么错误,也没个错误的信息,最好把报什么错说清楚,别人才好帮你解答