【问题标题】:setText crashes my app - inStream.readsetText 使我的应用程序崩溃 - inStream.read
【发布时间】:2013-09-05 08:45:30
【问题描述】:

我对 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





    }
}

为什么要**parameter.setText(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 "";
    }
}

如果在 readData() 我写 return "test"; 然后工作

如果在 readData() 我写 return byteArray.toString(); 然后工作

如果在 readData() 我写 for( i = 0; i 然后工作

如果在 readData() 我写 for( i = 0; i 然后 崩溃

int available = 13 在这种情况下。

我的问题对我来说是不合逻辑的。请指教。

感谢所有回答

【问题讨论】:

  • 我认为这是一个错字,但arameter = (TextView) view.findViewById( R.id.gridItemParameter ); 的名称缺少 p,就像在 parameter 中一样
  • 你可以试试 parameter.setText(String.valueOf(data));我不确定,但可能会起作用。我假设“参数”对象不为空。
  • 你能粘贴你得到的错误吗?
  • paste2.org/vJ5fOPvO - 错误。但它不为空 - Toast.makeText 工作良好。当然它是参数而不是参数 - 对不起。 String.valueOf(data) - 不工作

标签: android crash stream textview settext


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我猜你的 FINAL STRING 数据

    使数据无法填写

    所以你的变量 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();
    

    【讨论】:

    • 我有>String Data = "";
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 1970-01-01
    相关资源
    最近更新 更多