简体   繁体   中英

Android: SetText in handler doesn't change text in TextView

I have an app that connects to bluetooth and displays information received. when pressing back, the activity isn't killed and I can go back to the activity and everything is the same. The activity collects the data in the background and everything is okay I have a button, that when pressed, kills all activities (including the activity that displays the data). When I try to create that activity again, the data keeps coming, and I can still handle it and write it to a file, but it won't show it in the UI. It seems the setText function in the handler doesn't work after the activity is created the second time (setText does work outside of the handler).

The boolean value synced is true if the activity is created a second time. This is OnCreate

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);
        loading_panel = findViewById(R.id.loadingPanel);
        loading_panel.setVisibility(View.GONE);
        connectionButton = (Button) findViewById(R.id.connection_button);

        gsrTextView = (TextView) findViewById(R.id.gsr_textview);
        emgTextView = (TextView) findViewById(R.id.emg_textview);
        adxlTextView = (TextView) findViewById(R.id.adxl_textview);
        fsrTextView = (TextView) findViewById(R.id.fsr_textview);
        textviews_set=true;
        if(synced)
        {
            setConnected(currMac);
            manageConnection();
            Button sync_button = (Button) findViewById(R.id.sync_button);
            sync_button.setText(R.string.synced_text);
        }
        // more code
    }

Implementation of OnBackPressed :

@Override
    public void onBackPressed() {
        Intent i = new Intent(AdminActivity.this, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(i);
    }

This is (part of) the handler:

@SuppressLint("HandlerLeak")
    public final Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                //a lot of code for other cases
                case READ_SUCCESS:
                    String s = (String) msg.obj;
                    showData(s);
                    
                    break;
            }
        }
    }

This is (part of) showData : private void showData(String s) { // todo: 1.parse the json; 2.show data (json components) on the textViews

    try {
        //Log.d(LOG_TAG, "the new string is " + s);
        JSONObject json = new JSONObject(s);

        // Collect the sensors measurements
        String gsrMeasure = json.getString("gsr");
        String emgMeasure = json.getString("emg");
        String adxlMeasure = json.getString("adxl");
        String fsrMeasure = json.getString("fsr");
        String time_str = json.getString("time");

        // Show the data
        if(textviews_set) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.d(LOG_TAG, "ABOUT TO SET TEXT");
                    gsrTextView.setText(gsrMeasure);
                    emgTextView.setText(emgMeasure);
                    adxlTextView.setText(adxlMeasure);
                    fsrTextView.setText(fsrMeasure);
                }
            });
        }
        //more code
    }
}

I even made sure that I'm running the setText on UI thread even though it works on the first run of the activity without the runOnUiThread I also thought that maybe the handler is called before the findViewByID s are called but I used a boolean variable textviews_set to make sure it doesn't happen

It is worth mentioning that when I create the activity for the first time, the bluetooth device isn't connected and thus the handler isn't running, so there's a really good chance that the problem lies there. I just don't know exactly where, or how to solve it.

Based on the information provide the follow is the best suggestion.

So try the following in onCreate() before textviews_set=true;

   gsrTextView.setText(" ");
   emgTextView.setText(" "); 
   adxlTextView.setText(" "); 
   fsrTextView.setText(" "); 

Hope this solves issue.

Reference:

I found out what the problem was, if anyone gets stuck with the same issue. I declared all the textviews as non-static, thus when creating the activity the second time, for some reason the handler still referred to the textviews of the first instance of the activity. All I had to do was declare the textviews in the class as static , so when I create the activity for the second time, the textviews are already declared and refer to the actual textviews in the xml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM