androidmultithreadingviewroot

ViewRoot$CalledFromWrongThreadException on android app


I have read some threads regarding this and I did already take steps to resolve it. I am using a handler (so that I don't update the UI on a separate thread) and so far I can't understand why this is still happening.

public class MyApp extends Activity implements OnClickListener, Runnable {

    private ViewSwitcher switcher;
    private static final int REFRESH_SCREEN = 1;
    private boolean isValid = false;
    private ProgressDialog dialog;
    private TextView errorMessage;      

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(this);

        TextView errorMessage = (TextView)findViewById(R.id.txtErrorMessage);
        errorMessage.setVisibility(View.GONE);

        switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);
    }

    public void onClick(View v)  
    {
        isValid = false;
        dialog = ProgressDialog.show(ConcentraApp.this, "", "Loading. Please wait...", true);

        Thread thread = new Thread(this);
        thread.start();
    }

    public void run() {
        String username = ((TextView)findViewById(R.id.txtUsername)).getText().toString();
        String password = ((TextView)findViewById(R.id.txtPassword)).getText().toString();
        errorMessage = (TextView)findViewById(R.id.txtErrorMessage);
        errorMessage.setVisibility(View.GONE);

        /* ... contact web service and get response ..*/

        try {
            /* get result from web service */
            isValid = Boolean.parseBoolean(result);
            if(isValid)
            {
                handler.sendEmptyMessage(1);                
            }
            else
            {
                handler.sendEmptyMessage(0);    
            }

        } catch (Exception e) {
            handler.sendEmptyMessage(2);
            isValid = false;        
        }                                             
    }

    private Handler handler = new Handler() {
         @Override
         public void handleMessage(Message msg) {
             if(msg.what == 1)
             {
                 errorMessage.setVisibility(View.VISIBLE);
                 errorMessage.setText("Correct login");
                 switcher.showNext();
             }
             else if(msg.what == 0)
             {
                 errorMessage.setVisibility(View.VISIBLE);
                 errorMessage.setText("Invalid login");
             }
             else
             {
                 errorMessage.setVisibility(View.VISIBLE);
                 errorMessage.setText("Internet error");
             }
             dialog.dismiss();
         }
        };
}

I am very new to this so I wouldn't be surprised if I'm missing something obvious. It works fine without the thread, but then the process dialog doesn't show.

Many thanks in advance


Solution

  • You cannot call this:

    errorMessage.setVisibility(View.GONE);
    

    From a background thread. You should do it via handler as well.