android socket 闪退,android客户端闪退
老師,我把客戶端的代碼放在android程序里,給一個button監聽事件,就是調用這個socket方法,但是一點button就閃退了,沒連接上~protected?void?onCreate(Bundle?savedInstanceState)?{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
btn=(Button)?findViewById(R.id.button2);
btn.setOnClickListener(new?OnClickListener()?{
@Override
public?void?onClick(View?v)?{
initClientSocket();
}
});
}
@Override
public?boolean?onCreateOptionsMenu(Menu?menu)?{
//?Inflate?the?menu;?this?adds?items?to?the?action?bar?if?it?is?present.
getMenuInflater().inflate(R.menu.client,?menu);
return?true;
}
@Override
public?boolean?onOptionsItemSelected(MenuItem?item)?{
//?Handle?action?bar?item?clicks?here.?The?action?bar?will
//?automatically?handle?clicks?on?the?Home/Up?button,?so?long
//?as?you?specify?a?parent?activity?in?AndroidManifest.xml.
int?id?=?item.getItemId();
if?(id?==?R.id.action_settings)?{
return?true;
}
return?super.onOptionsItemSelected(item);
}
private?void?initClientSocket()?{
try?{
Socket?socket=new?Socket("192.168.191.1",8888);
OutputStream?os=socket.getOutputStream();
PrintWriter?pw=new?PrintWriter(os);
pw.write("nihao");
pw.flush();
socket.shutdownOutput();
pw.close();
os.close();
socket.close();
}?catch?(UnknownHostException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}?catch?(IOException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
}
}
總結
以上是生活随笔為你收集整理的android socket 闪退,android客户端闪退的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql无法添加或更新子行_MySQL
- 下一篇: 计算机裸机与应用程序及用户之间的桥梁是,