Reputation: 1504
Dear Folks,
I am currently working on a android app where I need to show the progress dialog. I need to have a context object for this.
This task I am implementing in the class which extends the application. But every time when I tried to access context object it is showing me the WindowManager.BadTokenException
for this line
Dialog dialog=ProgressDialog.show(getApplicationContext(), "Status", "Downloading The master");
Please help me where I am doing wrong
Edit: Below is my code!
public class FlightStatus extends Application {
private Context context;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
SharedPreferences preference=getSharedPreferences("FlightStatus", 1);
if(preference.getBoolean("firstLaunch", true))
{
try {
Dialog dialog=ProgressDialog.show(getApplicationContext(), "Status!", "Downloading The master!!");
XLSReadHelper excelReader=new XLSReadHelper(getApplicationContext());
//excelReader.readExcelFile(Environment.getExternalStorageDirectory()+"/UHCPAudit/cases.xls");
excelReader.readExcelFile();
Log.d("Excel Operation ", "records read!!");
preference.edit().putBoolean("firstLaunch", false).commit();
dialog.dismiss();
} catch (Exception e) {
// TODO: handle exception
Log.d("Excel Operation ", e.getMessage());
}
}
}
}
Upvotes: 0
Views: 173
Reputation: 30611
It is incorrect to try getting Context
inside Application
, as no Activity
is yet initialized at this stage. Instead show your dialog from Activity
/Fragment
.
If you are showing the progress dialog from an Activity
, use:
Dialog dialog = ProgressDialog.show(MyActivity.this, "Status", "Downloading The master");
If you are showing it from a Fragment
, then first add this member to your Fragment
class:
private Activity activity;
Then call this in your onCreateView()
method:
activity = getActivity();
and finally
Dialog dialog = ProgressDialog.show(activity, "Status", "Downloading The master");
Upvotes: 1
Reputation: 3000
You can use a constructor which takes the Activity as context
public class YourClass extends Application{
private Activity context;
public YourClass(Activity context){
this.context = context;
}
}
And then from your calling Activity
:
YourClass yourClass = new YourClass(this);
You can also try to use the ProgressDialog
in your Activity
itself, in a method which you can call from your custom class:
public void showProgress(){
//Your progress code
}
And in your custom class:
context.showProgress();
Upvotes: 0