Android setText使我的应用程序崩溃-inStream.read
我对setText(文本视图)有问题Android setText使我的应用程序崩溃-inStream.read,android,crash,stream,textview,settext,Android,Crash,Stream,Textview,Settext,我对setText(文本视图)有问题 view=EgridView.getChildAt(迭代器); 参数=(TextView)view.findViewById(R.id.gridItemParameter); 如果(modbus.readAvailable()>0){ 如果(!((data=modbus.readData()).equals(“”)){ Log.i(“--------------TEST------”,data);//您只能在UI线程上修改UI元素,例如TextView 从a
view=EgridView.getChildAt(迭代器);
参数=(TextView)view.findViewById(R.id.gridItemParameter);
如果(modbus.readAvailable()>0){
如果(!((data=modbus.readData()).equals(“”)){
Log.i(“--------------TEST------”,data);//您只能在UI线程上修改UI元素,例如TextView
从android文档中查看此链接:我猜您的最终字符串数据
使数据无法填充
所以你的变量数据仍然是空的
您尝试设置text(null),这就是为什么
尝试将最终字符串数据更改为字符串数据=“”
或
如果readData函数有问题
试试这个解决方案
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(con, "Return Nya = " +sb.toString(), 0).show();
Log.v("TEST" , "Return Nya = " + sb.toString());
is.close();
我相信这是一个输入错误,但是parameter=(TextView)view.findViewById(R.id.gridItemParameter);
name缺少p,就像parameter
中一样,你可以试试parameter.setText(String.valueOf(data));我不确定,但可能有效。我假设“parameter”对象不是空的。你能粘贴你得到的错误吗?-错误。但它不是空的-Toast.makeText工作正常。当然它不是参数-抱歉。String.valueOf(data)-不工作我有>String data=“”;
public int readAvailable(){
try{
return inStream.available();
} catch( Exception e ){
return 0;
}
}
public String readData(){
try{
if( isConnected == true && socket.isConnected() && inStream != null ){
int i;
int oneByte;
byte byteArray[] = new byte[ 100 ];
int available = inStream.available();
String data = "";
if( available > 0 ){
inStream.read( byteArray );
for( i = 0; i < available; i++ ){
oneByte = byteArray[ i ] & 0xff;
data = data.concat( Integer.toString( oneByte ) + " " );
}
return data; // <-----
} else {
return "";
}
} else {
errorText = "no communication";
return "";
}
} catch( Exception e ){
errorText = e.getMessage();
return "";
}
}
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(con, "Return Nya = " +sb.toString(), 0).show();
Log.v("TEST" , "Return Nya = " + sb.toString());
is.close();