【发布时间】:2015-06-29 13:10:37
【问题描述】:
大家好,我有这段代码可以在 sharedprefs 中保存一些数据,但 android studio 告诉我“首选项管理器中的 getDefaultSharedPrefs 不能应用于 com.foo.downloadDB.AttemptLogin”我该如何解决这个错误?
我似乎无法从“子”类保存到我的 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 类中有这个方法
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 等对象记录正确..
【问题讨论】:
标签: javascript java android android-studio sharedpreferences