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?
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:
Secondly, define your handler in the activity class, not in the onClickListener.