从 class 保存到 sharedPrefs
Save in sharedPrefs from a class
大家好,我有这段代码可以在 sharedprefs 中保存一些数据,但是 android studio 告诉我 "getDefaultSharedPrefs in Preference Manager cannot be applied to com.foo.downloadDB.AttemptLogin" 我该如何解决这个错误?
我好像无法从 "child" class 保存到我的 sharedprefs
这是代码:
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 中有这个方法 class
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,但是当我尝试从相应的 Fragment 访问它时它不起作用..这是我的片段代码:
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和其他对象正确记录..
这是问题所在:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
这是 AsyncTask 的实例,而不是 Activity
只用downloadDB.this
代替这个;但请使用大写 (DownloadDB
)
调用您的 class
大家好,我有这段代码可以在 sharedprefs 中保存一些数据,但是 android studio 告诉我 "getDefaultSharedPrefs in Preference Manager cannot be applied to com.foo.downloadDB.AttemptLogin" 我该如何解决这个错误?
我好像无法从 "child" class 保存到我的 sharedprefs 这是代码:
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 中有这个方法 class
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,但是当我尝试从相应的 Fragment 访问它时它不起作用..这是我的片段代码:
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和其他对象正确记录..
这是问题所在:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
这是 AsyncTask 的实例,而不是 Activity
只用downloadDB.this
代替这个;但请使用大写 (DownloadDB
)