Reputation: 139
I have few problems with syntax errors here. Below I have an alertDialog
with sharedPreference
and if you are wonder why I have this in a asynctask you can see this Shared Prefence for alert dialog is making my application non responsive But the syntax problem is underneath installed
in my onPostExecute
and it says "installed
cannot be resovled to a variable." I also have another one under settings
in my onPostExecute
that says the same thing as well. I know this may seem like an easy fix to some people but I find this really complicated for someone like me. I also advice you look at the link if you have a better method than the AsyncTask
and onResume()
@Override
protected void onResume() {
class asynctask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... arg0) {
final SharedPreferences settings = getSharedPreferences("pref_name", 0);
boolean installed = settings.getBoolean("installed", false);
return null;
}}}
protected void onPostExecute(String file_ad) {
if(!installed){
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Title");
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.setAdapter(new MyAdapter(), null);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("installed", true);
editor.commit();
}
});
alertDialog.show();
}
super.onResume();
}
Upvotes: 0
Views: 64
Reputation: 3099
installed
is declared only in doInBackground
, so it's not visible into onPostExecute
.
Instead of returning a String
in doInBackground
(where in fact you always return null
), you should return a boolean
(which would be installed
), and in onPostExecute
change the parameter for a boolean
and test it instead of testing installed
.
EDIT
Taking more time to update my answer :
First, I agree with @codeMagic, you definitely DON'T need an AsyncTask
to do that !
But if you really want to do it this way, here is how you could do it : first, you should change the signature of your AsyncTask
this way :
class asynctask extends AsyncTask<String, String, Boolean> {
Then in doInBackground
, change the return value and instead of returning null
, return installed
:
@Override
protected boolean doInBackground(String... arg0) {
final SharedPreferences settings = getSharedPreferences("pref_name", 0);
boolean installed = settings.getBoolean("installed", false);
return installed;
}}}
And finally, change the parameter of onPostExecute
to accept a boolean
:
protected void onPostExecute(boolean installed) {
This way, you can test installed
in your method.
Upvotes: 1
Reputation: 54790
The installed
variable is local to the doInBackground
method, onPostExecute
cannot see it. You can either add an instance variable:
class asynctask extends AsyncTask<String, String, String> {
boolean installed = false;
@Override protected String doInBackground(String... arg0) {
installed = true;
}
}
Or, you can change the signature of your Async Task so that it returns a boolean, then simply return installed from doInBackground
and it will be passed in as the parameter to onPostExecute
:
class asynctask extends AsyncTask<String, String, Boolean> {
@Override protected Boolean doInBackground(String... arg0) {
return true;
}
protected void onPostExecute(Boolean installed) {
}
}
Upvotes: 0