保存在类的sharedPrefs中

时间:2015-06-29 13:10:37

标签: javascript java android android-studio sharedpreferences

大家好我已经有了这个代码来保存sharedprefs中的一些数据,但android studio告诉我" Preference Manager中的getDefaultSharedPrefs不能应用于com.foo.downloadDB.AttemptLogin"我该怎么做才能解决这个错误?

我好像无法从一个孩子那里拯救我的共享信息。类 这是代码:

public class downloadDB extends Activity  {

    private EditText user, pass;
    private android.widget.Button mSubmit, mRegister;

    // Progress Dialog
    private ProgressDialog pDialog;
    //JSON parser class
    JSONParser jsonParser = new JSONParser();

    //php login script location:

    //localhost :
    //testing on your device
    //put your local ip instead,  on windows, run CMD > ipconfig
    //or in mac's terminal type ifconfig and look for the ip under en0 or en1
    // private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/login.php";

    //testing on Emulator:
    private static final String LOGIN_URL = "http://www.myurl.com/users.php";

    //testing from a real server:
    //private static final String LOGIN_URL = "http://www.yourdomain.com/webservice/login.php";

    //JSON element ids from repsonse of php script:
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";



    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.download_db);
        new AttemptLogin().execute();


    }


   public class AttemptLogin extends AsyncTask<String, String, String>  {

        boolean failure = false;
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor1 = sharedPreferences.edit();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(downloadDB.this);
            pDialog.setMessage("Download..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String...args) {


            //Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();


            Log.d("request!", "starting");
            // getting product details by making HTTP request
            JSONObject json = jsonParser.makeHttpRequest(
                    LOGIN_URL, "POST", params);

            //check your log for json response
            Log.d("Login attempt", json.toString());

            editor1.putString("JSON_DB", json.toString());
            editor1.commit();
            System.out.println(editor1.commit());

            return null;
        }

        // After completing background task Dismiss the progress dialog

        protected void onPostExecute(String file_url) {
            //dismiss the dialog once product deleted
            pDialog.dismiss();
            if (file_url != null) {
                Toast.makeText(downloadDB.this, file_url, Toast.LENGTH_LONG).show();
            }

            // Intent intent = new Intent(downloadDB.this, SampleActivity.class);
            // startActivity(intent);
        }
    }
}

修改

我的SampleActivity类

中有这个方法
private void LoadPreferences(){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    String  name = sharedPreferences.getString("name", "0") ;
    String  email = sharedPreferences.getString("email", "0") ;
    String  id = sharedPreferences.getString("id", "0") ;
    String  Database = sharedPreferences.getString("JSON_DB", "0") ;
    Toast.makeText(this, Database, Toast.LENGTH_LONG).show();
    //Toast.makeText(this, id, Toast.LENGTH_LONG).show();
    //Toast.makeText(this, email, Toast.LENGTH_LONG).show();
}

并且Toast工作正常,它正确地打印了JSON_DB,但是当我尝试从相应的片段到达它时它不起作用..这是我的片段代码:

public class SampleFragment extends Fragment {


    public static String KEY_ID = "id";
    public static String KEY_EMAIL = "email";
    public static String KEY_NAME = "name";
    public static String JSON_DB = "";

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        LoadPreferences();

        Log.i("string", KEY_EMAIL);
        Log.i("string", KEY_ID);
        Log.i("string", KEY_NAME);
        Log.i("string", JSON_DB);

和KEY_EMAIL,KEY_ID和其他对象正确记录..

1 个答案:

答案 0 :(得分:0)

这是问题所在:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

这是AsyncTask的实例,而不是Activity

仅使用downloadDB.this而不是此;但请使用大写字母(DownloadDB

给您的班级打电话