1
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bluetooth_datadisplay);


        txtString = (TextView) findViewById(R.id.txtString);
        txtStringLength = (TextView) findViewById(R.id.testView1);
        sensorView0 = (TextView) findViewById(R.id.sensorView0);
        sensorView1 = (TextView) findViewById(R.id.sensorView1);
        sensorView2 = (TextView) findViewById(R.id.sensorView2);
        sensorView3 = (TextView) findViewById(R.id.sensorView3);

     bluetoothIn = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == handlerState) {
                String readMessage = (String) msg.obj;
                recDataString.append(readMessage);
                int endOfLineIndex = recDataString.indexOf("~");
                if (endOfLineIndex > 0) {
                    String dataInPrint = recDataString.substring(0, endOfLineIndex);
                    txtString.setText("Data Received = " + dataInPrint);
                    int dataLength = dataInPrint.length();
                    txtStringLength.setText("String Length = " + String.valueOf(dataLength));

                    if (recDataString.charAt(0) == '#')
                    {
                        String sensor0 = recDataString.substring(1, 5);
                        String sensor1 = recDataString.substring(6, 10);
                        String sensor2 = recDataString.substring(11, 15);
                        String sensor3 = recDataString.substring(16, 20);

                        sensorView0.setText(" Sensor 0 Voltage = " + sensor0 + "V");
                        sensorView1.setText(" Sensor 1 Voltage = " + sensor1 + "V");
                        sensorView2.setText(" Sensor 2 Voltage = " + sensor2 + "V");
                        sensorView3.setText(" Sensor 3 Voltage = " + sensor3 + "V");
                    }
                    recDataString.delete(0, recDataString.length());

                }
            }
        }
    };
  }

Hi I getting this warning of handler could be static or leaks might occur. I had look at other post but it doesn't help me to solve my problem. :( The warning happened at the start of bluetoothIn to the end.

rederror after moving out of on createhandler is in grey

Spotty
  • 197
  • 2
  • 2
  • 14

1 Answers1

0

possible duplicate this question. So the solution is below:

    static class BluetoothInHandler extends Handler {
            private final WeakReference<YourActivityName> mActivity; 

            IncomingHandler(YourActivityName activity) {
                mActivity= new WeakReference<YourActivityName>(activity);
            }
            @Override
            public void handleMessage(Message msg)
            {
                final YourActivityName thizz = mActivity.get();
                if (thizz == null) {
                    return;
                 }
                if (msg.what == thizz.handlerState) {
                  // Do what you need
                 }
            }
    }
Community
  • 1
  • 1
NamNH
  • 1,752
  • 1
  • 15
  • 37
  • Hi @john it doesn't work. modified static is not allow on the oncreate block. I had tried it. – Spotty Dec 23 '15 at 02:59
  • this is the inner class. so try to move it out of `onCreate` method. And instance it in your oncreate like `BluetoothInHandler handler = new BluetoothInHandler(this)` – NamNH Dec 23 '15 at 03:07
  • Hi @John. I attached image showing the error. I did what you say, but it doesn't work. – Spotty Dec 23 '15 at 03:54
  • btw this part IncomingHandler(YourActivityName activity) incoming handler is underline with red as well. – Spotty Dec 23 '15 at 03:58
  • Put `mActivity.` before the activity variable or method, ex: `activity.handlerState`....Let chat from here http://chat.stackoverflow.com/rooms/98757/discusion – NamNH Dec 23 '15 at 04:04
  • Hi @John I don't have enough reputation to chat in the discussion. is there other way I can contact you?? – Spotty Dec 23 '15 at 04:23
  • Is there any problem now? – NamNH Dec 23 '15 at 04:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98759/discussion-between-john-and-spotty). – NamNH Dec 23 '15 at 04:26