これが私のコードです。ここでは、キャプション付きのProgressBarを使用してスレッドを実行しています。
スレッドのrun()で、インターネット接続が機能している場合、ELSE部分が実行され、そのProgressBarが閉じられます。しかし、インターネット接続が機能していない場合、patが実行されると、ProgressBarを閉じようとするとnull例外が発生します。nullチェックif(m_ProgressDialog == null)も追加し、DialobBoxをNULLで出力します。
私のコードはどうなっていますか?ProgressBarはELSE部分で却下されますが、IFではNULL例外がスローされます。
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();
}