0

I have a problem with setText( TextView ).

view        = EgridView.getChildAt( iterator );
parameter   = (TextView) view.findViewById( R.id.gridItemParameter );


if( modbus.readAvailable() > 0 ){
    if( !((data = modbus.readData()).equals("")) ){





Log.i("-------------TEST-----------", data); // <-------- wrok
//Toast.makeText(getApplicationContext(), data , Toast.LENGTH_LONG).show(); <----- work
// parameter.setText( "test" ); <----------- work
parameter.setText( data ); // <--------- crash





    }
}

Why **parameter.setText( data ); **crashes my app?


More code:

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 "";
    }
}

If in readData() I write return "test"; then work

If in readData() I write return byteArray.toString(); then work

If in readData() I write for( i = 0; i < 10; i++ ){ then work

If in readData() I write for( i = 0; i < 11; i++ ){ then crashes

int available = 13 in this situation.

My problem is illogical for me. Please advice.

Thanks for all answers

4

2 回答 2

0

您只能在 UI 线程上修改 UI 元素,例如 TextView。

从 android 文档中查看此链接:http: //developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)

于 2013-09-05T09:02:04.550 回答
0

我猜你的最终字符串数据

使数据无法填写

所以你的变量 Data 仍然为空

并且您尝试 setText (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();
于 2013-09-05T09:41:37.347 回答