Here is my code Below. Here i m running a thread with a ProgressBar with a caption.
In run() of thread, if my internet connection is working then the ELSE part gets execute & dismiss that ProgressBar. But if my internet connection is not working then IF pat gets execute, here i m getting null exception when i try to dismiss ProgressBar. I have also added is null check if(m_ProgressDialog == null), and prints DialobBox in NULL..
Whats happening with my code.? ProgressBar dismissing in ELSE part but in IF is throw NULL Exception…
public void onCreate(Bundle savedInstanceState) {
.....................
.....................
//some CODE //
.....................
.....................
viewOrders = new Runnable(){
@Override
public void run() {
try {
if(!utilityFunctions.HaveNetworkConnection(SplashScreenActivity.this)) //checking internet connection
{
Log.i(UtilityFunctions.APP_TAG, "NO Connecttion");
//updateUI(0);
if(m_ProgressDialog == null)
Log.i(UtilityFunctions.APP_TAG, "DialogBox is NULL");
else
m_ProgressDialog.dismiss();
Log.i(UtilityFunctions.APP_TAG, "Dismissed");
handler.sendEmptyMessage(0);
}
else
{
try {
m_ProgressDialog.dismiss();
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Intent mainIntent = new Intent().setClass(SplashScreenActivity.this, MainActivity.class);
Log.i(UtilityFunctions.APP_TAG, "Starting Program");
startActivity(mainIntent);
finish();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg) {
//super.dispatchMessage(msg);
super.handleMessage(msg);
updateUI(msg.what);
}
};
};
Thread thread = new Thread(null, viewOrders, "MagentoBackground");
thread.start();
m_ProgressDialog = ProgressDialog.show(SplashScreenActivity.this, "Please wait...", "Getting required data from server. This is an one time activity...", true);
}
public void updateUI(int code){
Log.i(UtilityFunctions.APP_TAG, "updateUI");
if(code == 0)
Toast.makeText(SplashScreenActivity.this, "Unable to verify application signature. Please Check your internet connection & try again", Toast.LENGTH_LONG).show();
else
Toast.makeText(SplashScreenActivity.this, "Unable to process request. ", Toast.LENGTH_LONG).show();
}
Try to declare
ProgressDialog()at the topHope this helps you..