在两个线程里 listView.setAdapter(simpleAdapter); 会使进程挂掉?

谁学逆向工程 2016-12-21 04:36:29
在程序启动的时候在右上角初始化了菜单项,点击菜单项执行下面的函数。
循环会执行两次,两次的内容由Windows C# 程序发送过来。而C#是从MySQL读取出来的。
我单步跟踪了第一轮循环,除了图片无法监视是否接收正确,其余的字符串变量和数值变量都正确接收。
第二轮循环完毕之后,最后执行了 listView.setAdapter(simpleAdapter); 此时手机程序界面闪退。
我这找不到毛病在哪了,哪位能帮忙看看。
void LoadPCMenu() {
new Thread() {
public void run() {
try {
Socket s = new Socket(strIP, Integer.parseInt(strPORT));
OutputStream out = s.getOutputStream();
out.write("1".getBytes());//发送1表示想要读取菜单
InputStream isStream = s.getInputStream();
OutputStream osStream = s.getOutputStream();

byte[] bRowNumber = new byte[4];
isStream.read(bRowNumber);//接收行数

int iRowNumber = ByteArrayToInt(bRowNumber); //把行数转换成int类型


datalist.clear();
int n=0;
byte []bLaJi = new byte[2];
String tempFenLei = "";// 每次收到分类都和这个比较,如果不同就把新分类保存到这里
for (int i = 0; i < iRowNumber; i++) {
byte[] bFenLei = new byte[1024];
n = isStream.read(bFenLei, 0, 4);
n = isStream.read(bFenLei, 0, 4);
int ibfLen = ByteArrayToInt(bFenLei);
bFenLei = new byte[ibfLen];
isStream.read(bFenLei, 0, ibfLen);
String strFenLei = new String(bFenLei, "UTF-8");//分类
strFenLei = strFenLei.trim();
if (!tempFenLei.equals(strFenLei))
tempFenLei = strFenLei;
tempFenLei.trim();
osStream.write(bLaJi);//发送垃圾数据表示接收完毕


byte[] bMingCheng = new byte[1024];
n = isStream.read(bMingCheng, 0, 4);
n = isStream.read(bMingCheng,0,4);
ibfLen = ByteArrayToInt(bMingCheng);
bMingCheng = new byte[ibfLen];
isStream.read(bMingCheng, 0, ibfLen);
String strMingCheng = new String(bMingCheng, "UTF-8");//名称
strMingCheng = strMingCheng.trim();
osStream.write(bLaJi);//发送垃圾数据表示接收完毕


byte[] bJiaGe = new byte[1024];
n = isStream.read(bJiaGe, 0, 4);
n = isStream.read(bJiaGe, 0, 4);
ibfLen = ByteArrayToInt(bJiaGe);
bJiaGe = new byte[ibfLen];
isStream.read(bJiaGe, 0, ibfLen);
double dJiaGe = bytes2Double(bJiaGe);
String strJiaGe = String.valueOf(dJiaGe);//价格
strJiaGe = strJiaGe.trim();
osStream.write(bLaJi);//发送垃圾数据表示接收完毕


byte[] bTuPianSize = new byte[4];
n = isStream.read(bTuPianSize,0,4);
n = isStream.read(bTuPianSize, 0, 4);
n = isStream.read(bTuPianSize, 0, 4);//接收图片大小
int iTuPianSize = ByteArrayToInt(bTuPianSize);
osStream.write(bLaJi);//发送垃圾数据表示接收完毕


byte[] bImage = new byte[iTuPianSize];
int offset = 0;
while (offset < iTuPianSize) {
n = isStream.read(bImage, offset, iTuPianSize - offset);//接收二进制形式的图片
offset += n;
}
osStream.write(bLaJi);//发送垃圾数据表示接收完毕

Bitmap tupian = BitmapFactory.decodeByteArray(bImage, 0, bImage.length);//把二进制形式的图片转换成 Bitmap 类型



Map<String, Object> map = new HashMap<String, Object>();
map.put("图片", tupian);
map.put("分类", strFenLei);
map.put("名称", strMingCheng);
map.put("价格", strJiaGe);
datalist.add(map);
}
s.close();
} catch (Exception ee) {
Toast.makeText(getApplicationContext(), ee.getMessage(), Toast.LENGTH_SHORT).show();
}
simpleAdapter = new SimpleAdapter(MainActivity.this,
datalist,
R.layout.menuitem,
new String[]{"图片","分类","名称", "价格"},
new int[]{R.id.imageView, R.id.CaiPinFenLei, R.id.CaiMing, R.id.JiaGe});
listView.setAdapter(simpleAdapter);
}
}.start();
}
...全文
281 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
谁学逆向工程 2016-12-22
  • 打赏
  • 举报
回复
引用 5 楼 Mailbomb 的回复:
使用Handler处理吧,在非UI线程中不能进行UI操作
怎么用
网络咖啡 2016-12-22
  • 打赏
  • 举报
回复
使用Handler处理吧,在非UI线程中不能进行UI操作
谁学逆向工程 2016-12-22
  • 打赏
  • 举报
回复
引用 3 楼 zhumj_zhumj 的回复:
UI需要到UI线程操作,你用hundler抛出再操作UI吧
怎么抛出,抛异常?
头发还没秃a 2016-12-22
  • 打赏
  • 举报
回复
UI需要到UI线程操作,你用hundler抛出再操作UI吧
寒冰大神 2016-12-22
  • 打赏
  • 举报
回复
runOnUiThread(new Runnable() { @Override public void run() { listView.setAdapter(simpleAdapter); } });
developerzjy 2016-12-22
  • 打赏
  • 举报
回复
把线程里面的UI操作换成sendMessage发送消息给handler,然后handler处理相应的UI操作
谁学逆向工程 2016-12-21
  • 打赏
  • 举报
回复
引用 1 楼 xiaohanluo 的回复:
崩溃日志贴出来吧。
我用的Android studio,崩溃日志在哪个窗口显示?
王三的猫阿德 2016-12-21
  • 打赏
  • 举报
回复
崩溃日志贴出来吧。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧