簡體   English   中英

嘗試顯示字節值時,setText使應用程序崩潰

[英]setText crashes app when trying to display byte values

我正在開發可發送和接收數據的android應用程序。 在應用程序中,我有一個按鈕和一些texviews。 當我按下按鈕時,將發送數據(兩個字符)。 並且已發送的數據將在兩個tekst視圖中顯示。

我對兩個整數做了同樣的事情,現在我想對字節和字符做同樣的事情,但是失敗了。

logcat給出以下錯誤:10-28 09:27:19.338:E / AndroidRuntime(13138):android.content.res.Resources $ NotFoundException:字符串資源ID#0x0

以下是onClick lisener代碼:

  @Override
  public void onClick(View v) {

   // Control value
   ArrayOutput[0] = 'B';
   ArrayOutput[1] = 'B';


 //Creating TextView Variable
 TextView text = (TextView) findViewById(R.id.tv);

 //Creating TextView Variable
TextView statustext = (TextView) findViewById(R.id.status);


       //Sets the new text to TextView (runtime click event)
 text.setText("You Have click the button");


// Convert string to bytes
ArrayOutput[0] = ArrayRecieved[0];
ArrayOutput[1] = ArrayRecieved[1];

 final char Byte1 = (char) ArrayOutput[0];
 final char Byte2 = (char) ArrayOutput[1];

final TextView Xtext = (TextView) findViewById(R.id.xtext);              
final TextView Ytext = (TextView) findViewById(R.id.ytext);
Ytext.setText(Byte1);
Xtext.setText(Byte2);


 try
 {
   statustext.setText("Sending....");
   server.send(ArrayOutput);
         statustext.setText("Sending succes");
 } 
       catch (IOException e)
 {
      statustext.setText("Sending failed");
      Log.e("microbridge", "problem sending TCP message", e);
 }  
    }
 });

是否有人暗示問題可能出在哪里? 歡迎任何建議! 如果我需要提供更多信息,請這樣說。

更新資料

謝謝大家的建議! 對於onclick函數,它可以工作! 我嘗試對接收功能執行相同的操作。 當有可用數據時,將調用此事件處理程序功能。

當我使用setText函數時,它在幾個周期后崩潰了我的ap,在此函數中,我有三個settext操作。 僅第一個被調用(然后應用崩潰)。 當我更改這些操作的順序時,仍然只調用第一個。 可能是該應用顯示了第一個settext操作但崩潰了嗎? 我使用偽數據,因此在調用eventhandler函數時,未使用實際的接收到的數據,但在第一次操作后應用仍然崩潰。 有人建議嗎?

另一方面,每秒發送一次數據。

下面是onRecieve(事件處理程序)函數:

@Override
public void onReceive(com.example.communicationmodulebase.Client client, byte[] data)
{

    Log.e(TAG, "In handler!");

    //Control value
    ArrayRecieved[0] = 'C';
    ArrayRecieved[1] = 'B';

    if (data.length < 2){
        return;
    }

   // Set data that has been recieved in array
   //ArrayRecieved[0] = data[0];
   //ArrayRecieved[1] = data[1];

   char Byte1 = (char) ArrayRecieved[0] ;
   char Byte2 = (char) ArrayRecieved[1] ;

   TextView Xtext = (TextView) findViewById(R.id.xtext);                 
   TextView Ytext = (TextView) findViewById(R.id.ytext);
   Xtext.setText(""+Byte2);
   Ytext.setText(""+Byte1);

   TextView textRecvStatus = (TextView) findViewById(R.id.RecvStatusText);
   textRecvStatus.setText("In handler!");

   }

});

TextView有兩種方法,例如

TextView.setText(CharSequence) and TextView.setText(int).

1)第一個方法直接為TextView分配一個文本,該文本作為CharSequence傳遞(可以是String,StringBuffer,Spannable ...)
2)第二種方法搜索您在資源中定義的String資源,其中ID作為參數傳遞。

並且您將char作為參數傳遞。 char被強制轉換為int類型,並將其作為TextView.setText(int)調用,並搜索具有該int ID的資源字符串,該ID的值未在Resources中定義。

類型轉換charString一樣String.valueOf(char) ,並嘗試一次...

您使用的方法的簽名采用CharSequence,因此采用字符序列。 使用setText(someEmptyString + Byte1) ,您可以通過someEmptyString(您將其定義為“”)和Byte1的串聯創建一個字符序列。

設置一些變化

 Ytext.setText(""+Byte1);

setText()需要字符串或資源ID(int)。 如果要顯示數值,則需要將其轉換為字符串,即:

setText(String.valueOf(someInt));

嘗試如下:

Ytext.setText(""+Byte1);
Xtext.setText(""+Byte2);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM