dev_android
dev_android

Reputation: 8818

Android thread handler problem

I have using following code to set a thread on button action.

public void onCreate(Bundle savedInstanceState) {
  .........................
  ..........................
   btnUpdateNow.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

    Thread updateThread = new Thread() {

                            Handler uiHandle;

                            @Override
                            public void run() {
                                    GetDetailsUpdate getDetailsUpdate = new GetDetailsUpdate(
                                            strUserId, strPassword,
                                            strUDID,
                                            getApplicationContext());
                                    uiHandle.sendEmptyMessage(0);                               
                            }
                        };
                        updateThread.start();

      Handler uiHandler = new Handler(){
            @Override
            public void handleMessage (Message msg){
                loader.setVisibility(View.INVISIBLE);

            }
        };
    }
  }
}

But it is giving following error.

05-27 17:35:11.580: ERROR/AndroidRuntime(4555): Uncaught handler: thread Thread-11 exiting due to uncaught exception

05-27 17:35:11.580: ERROR/AndroidRuntime(4555): java.lang.NullPointerException

What is the problem in my code?

Upvotes: 0

Views: 944

Answers (2)

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43108

You have defined your Handler twice: in the thread class, and in the onClickListener. So, you initialize not the variable you're using. Follow the next steps:

Firstly, remove the declaration from here:

 Thread updateThread = new Thread() {

     Handler uiHandle;

Secondly, define your handler in the activity class, not in the onClickListener.

Upvotes: 1

senola
senola

Reputation: 782

Your updateThread is using the uiHandle:

uiHandle.sendEmptyMessage(0);

But in your whole Thread implementation this field variable is never set to anything so it is null.

Upvotes: 0

Related Questions