0

i am trying to access saved preferences from within an asynctask but i always keep getting the error "preferences can not be resolved". Any ideas? Here is a part of the code:

    public class Login extends SherlockActivity {
      SharedPreferences preferences;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);

        new LongOperationLogin(this).execute();
    }
}

class LongOperationLogin extends AsyncTask<String, Void, String> {
    private Login longOperationContext = null;    

    public LongOperationLogin(Login context) {
        longOperationContext = context;
    }



    @Override
    protected String doInBackground(String... params) {
    //THIS IS WHERE I NEED THE VALUE
        String username = this.preferences.getString("username", "n/a");
        try {
        //JSON fetching
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            Log.v("Error", "URL exc");
        } catch (IOException e) {
            e.printStackTrace();
            Log.v("ERROR", "IOEXECPTOIn");
        } catch (JSONException e) {
            e.printStackTrace();
            Log.v("Error", "JsonException");
        }
        return null;
    }


    @Override
    protected void onPostExecute(String result) {
        }
    }

    protected void onPreExecute() {
    }

 protected void onProgressUpdate(Void... values) {
    }
}

Thanks in advance! Robert

robs
  • 649
  • 4
  • 13
  • 28
  • 1
    Try acessing it with `Login.this.preferences` – A--C Dec 17 '12 at 20:33
  • @Squonk at least you have courtesy, I've seen people take my comments and turn them into answers. Even when I'm not sure I'm right. – A--C Dec 17 '12 at 20:35
  • Hi, thanks for the fast replies! I tried this but i get the error "No enclosing instance of the type Login is accessible in scope"... – robs Dec 17 '12 at 20:39
  • 2
    Is your AsyncTask an **inner** class? Make it so it's contained in the Login class. You might also want to change its modifier to private/protected. It depends where you need it, but to access the variable, you need to have it inside `Login` – A--C Dec 17 '12 at 20:40
  • I'm not quite sure what inner class means. All the code is in the Login class though – robs Dec 17 '12 at 20:42
  • Your AsyncTask needs to be either in its own .java file, or as an inner class of your Login class. Right now it appears that you have it as a separate class within the same file, which I don't think will compile. – FoamyGuy Dec 17 '12 at 20:42
  • 1
    Take the ending brace of the Login class and put it at the end of the last line in your code. – A--C Dec 17 '12 at 20:42
  • It seems that your AsyncTask is not an inner class, so try with `longOperationContext.preferences.getString("username", "n/a")`. Just pay attention to the modifier for preferences if the classes are defined in different packages. Anyway, it might be better to obtain the String value before invoking the AsyncTask instance and include it as an AynscTask constructor parameter instead of the Login instance. – randomuser Dec 17 '12 at 20:46

2 Answers2

3

You should access your preferece variable with Login.this.preferences. Also make sure that your AsyncTask is an inner class (contained in Login).

A--C
  • 36,351
  • 10
  • 106
  • 92
0

Try this

    String username = longOperationContext.preferences.getString("username", "n/a");

and make preferences field public

nandeesh
  • 24,740
  • 6
  • 69
  • 79