[Solved] Starting a thread closes my android application


The thing is you can not access Ui element in a non-Ui thread. So you must be getting a Runtime exception. You can use runOnUi() or a Handler to do it on Main Thread . Where runOnUi() is a method of Activity class and A Handler allows you to send and process Message and Runnable objects associated with a thread’s MessageQueue.

runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(ActivityMain.this,"Message",Toast.LENGTH_LONG).show();
        }
    });

Or you can Use Handler

 new Handler().post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(ActivityMain.this,"Message",Toast.LENGTH_LONG).show();
        }
    });

Just make sure that when you create a new Handler, it is bound to the thread / message queue of the thread that is creating it. So you must not create it inside non Ui thread .

solved Starting a thread closes my android application