如何在数组中存储接收的数据?

时间:2015-09-24 13:02:14

标签: java android arrays rfduino

我正在处理一个Android应用程序,该应用程序从RFDuino接收数据并将其显示在Line Chart上。

在我的Mainactivity中,broadcastReceiver在接收时将数据发送到方法addData

private final BroadcastReceiver rfduinoReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (RFduinoService.ACTION_CONNECTED.equals(action)) {
            upgradeState(STATE_CONNECTED);
        } else if (RFduinoService.ACTION_DISCONNECTED.equals(action)) {
            downgradeState(STATE_DISCONNECTED);
        } else if (RFduinoService.ACTION_DATA_AVAILABLE.equals(action)) {
            addData(intent.getByteArrayExtra(RFduinoService.EXTRA_DATA));
        }
    }
};

addData方法如下:

 public void addData(byte[] data) {

    View view = getLayoutInflater().inflate(android.R.layout.simple_list_item_2, dataLayout, false);

    TextView text1 = (TextView) view.findViewById(android.R.id.text1);

       String riz = HexAsciiHelper.bytesToHex(data);
       t1 = Integer.parseInt(riz, 16);
       String testString = String.valueOf(t1);
       text1.setText(testString);

        dataLayout.addView(view,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
    }

现在,我想将t1的值存储到数组中。问题是,当我尝试这样做时,会发生NullPointerException并且程序停止。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

声明ArrayList<Integer>并在执行空检查后存储t1值。

ArrayList <Integer> t1array = new ArrayList <Integer> ();
String riz = HexAsciiHelper.bytesToHex(data);

if (riz != null) {
    Integer t1 = t1 = Integer.parseInt(riz, 16);
    t1array.Add(t1);
}