我有一个调用 web 服务并检索 contentValue 对象的应用程序。该对象填充了最终作为 SharedPreferences 存储的字符串字符串值。我使用 AsyncTask 进行 web 服务调用,以免阻塞 UI 线程。Web 服务会填充 CV 对象。
问题出在我尝试使用共享首选项中的值时。共享首选项对象为空。应用程序在 AsyncTask 实例化并填充它之前尝试使用此对象。如何让应用程序等到 AsyncTask 完成?
CompID 是作为参数传递给 Web 服务的字符串。如果第一次安装应用程序时此变量为空,我会显示一个对话框,要求用户输入 compID。
if(nfcscannerapplication.getCompId() == null ||
nfcscannerapplication.getCompId().trim().equalsIgnoreCase("null")){
Log.e(TAG, "compid null***********");
showPasswordDialogBox();
}
ShowPasswordDialogBox(未恰当命名)获取 compID 并将其传递给 asyncCompOpt()。
public void asyncCompOpt(){
String[] paramsCompOpt = new String[]{nfcscannerapplication.getCompId()};
AsyncGetCompanyOptions agco = new AsyncGetCompanyOptions();
agco.execute(paramsCompOpt);
}
asyncCompOpt 调用 AsyncTask。
private class AsyncGetCompanyOptions extends AsyncTask<String, Void, ContentValues> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute()
{
progressDialog = ProgressDialog.show(EntryActivity.this,
"Connecting to Server"," Getting your company's options...", true);
};
@Override
protected ContentValues doInBackground(String... params) {
ContentValues cv = null;
try {
Log.e(TAG, "inside doInBackground");
cv = loginWebservice.getCompanyOptionsAndPhonenumbers(nfcscannerapplication.getCompId());
if(cv == null){
Log.e(TAG, "cv is null");
}else{
Log.e(TAG, "cv is not null");
}
} catch (Exception e) {
e.printStackTrace();
}
return cv;
}
@Override
protected void onPostExecute(ContentValues result)
{
super.onPostExecute(result);
if(progressDialog != null)
progressDialog.dismiss();
String tagTouchInterval = (String) result.get("10");
Log.e(TAG, "tagTouchInterval in onpostexecute = " + tagTouchInterval);
String savePassword = (String) result.get("20");
String allowChangeUser = (String) result.get("30");
String displayRotaDetails = (String) result.get("40");
String displayClientPhoneNumber = (String) result.get("50");
String displayClientKeysafe = (String) result.get("60");
String displayDoubleupCarer = (String) result.get("70");
String displayContacts = (String) result.get("80");
String autoLogout = (String) result.get("90");
String displayNotesDisplayNotes = (String) result.get("100");
String displayMeds = (String) result.get("110");
String rotaLogout = (String) result.get("120");
String displayActualTime = (String) result.get("130");
String qrcodeEnabled = (String) result.get("140");
String manualInput = (String) result.get("150");
String rotasOnly = (String) result.get("160");
appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(EntryActivity.this.getApplicationContext());
prefsEditor = appSharedPrefs.edit();
prefsEditor.putString("10", tagTouchInterval);
prefsEditor.putString("20", savePassword);
prefsEditor.putString("30", allowChangeUser);
prefsEditor.putString("40", displayRotaDetails);
prefsEditor.putString("50", displayClientPhoneNumber);
prefsEditor.putString("60", displayClientKeysafe);
prefsEditor.putString("70", displayDoubleupCarer);
prefsEditor.putString("80", displayContacts);
prefsEditor.putString("90", autoLogout);
prefsEditor.putString("100", displayNotesDisplayNotes);
prefsEditor.putString("110", displayMeds);
prefsEditor.putString("120", rotaLogout);
prefsEditor.putString("130", displayActualTime);
prefsEditor.putString("140", qrcodeEnabled);
prefsEditor.putString("150", manualInput);
prefsEditor.putString("160", rotasOnly);
prefsEditor.commit();
}//end of postExecute
我将发布 oncreate 的一部分,以便您查看事物的流程。该应用程序正在尝试在异步完成之前访问 SharedPreferences。我怎样才能解决这个问题?谢谢。
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nfcscannerapplication = (NfcScannerApplication) getApplication();
sIs = savedInstanceState;
if(nfcscannerapplication.getCompId() == null ||
nfcscannerapplication.getCompId().trim().equalsIgnoreCase("null")){
Log.e(TAG, "compid null***********");
showPasswordDialogBox();
}
setContentView(R.layout.entryscreen);
//loginWebservice = new LoginWebservice(this);
loginWebservice = nfcscannerapplication.loginWebservice;
userName = (EditText)findViewById(R.id.username);
passwordPin = (EditText)findViewById(R.id.password);
String isSavePassword = appSharedPrefs.getString("20", null);
isAllowChangeUser = appSharedPrefs.getString("30", null);