记录向对象添加随机空字符串值
Log adding random null string value to object
嗨,
我有一个存储一些到期日期的应用程序,我正在尝试使用 SharedPreferences 将数据传递给另一个 activity。正如您在 .gif 中看到的那样,当按下 "Add" 按钮时它会正确记录数据,但是当我需要保存数据并在着陆时正确显示时页面,它将这个随机 null 添加到保存的数据中。
你知道这个空值被添加到哪里了吗?
如果您有任何反馈意见或需要任何其他相关代码,请随时关注我!
FormActivity
import static tech.destinum.recorderis.adapters.FormAdapter.FORM_PREFERENCES;
public class Form extends AppCompatActivity {
private UserProfile mUserProfile;
private DBHelper mDBHelper;
private Auth0 mAuth0;
private Context mContext;
private RecyclerView mRecyclerView;
private ArrayList<Document> mArrayList;
private FormAdapter mAdapter;
private String soat, rtm, src, str, to;
private static final String PREFERENCES = "Preferences";
private static final String TAG = "Form Activity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view_form);
mArrayList = new ArrayList<>();
mArrayList.add(new Document(R.string.doc_soat, 0));
mArrayList.add(new Document(R.string.doc_rtm, 1));
mArrayList.add(new Document(R.string.doc_src, 2));
mArrayList.add(new Document(R.string.doc_str, 3));
mArrayList.add(new Document(R.string.doc_tao, 4));
mAdapter = new FormAdapter(mContext, mArrayList);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.form_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.confirmation:
createUser();
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
break;
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
//TODO: New method for premium version
//free version
private void createUser() {
mAuth0 = new Auth0(getString(R.string.auth0_client_id), getString(R.string.auth0_domain));
// The process to reclaim an UserProfile is preceded by an Authentication call.
AuthenticationAPIClient aClient = new AuthenticationAPIClient(mAuth0);
aClient.tokenInfo(CredentialsManager.getCredentials(this).getIdToken())
.start(new BaseCallback<UserProfile, AuthenticationException>() {
@Override
public void onSuccess(final UserProfile payload) {
Form.this.runOnUiThread(new Runnable() {
public void run() {
mUserProfile = payload;
mDBHelper = new DBHelper(getApplicationContext());
SharedPreferences mSP = getSharedPreferences(FORM_PREFERENCES, Context.MODE_PRIVATE);
for (int i = 0; i < 6; i++){
switch (i){
case 0:
soat += mSP.getString("soat", "");
Log.d(TAG, "Soat:" + soat);
break;
case 1:
rtm += mSP.getString("rtm", "");
Log.d(TAG, "RTM:" + rtm);
break;
case 2:
src += mSP.getString("src", "");
Log.d(TAG, "SRC:" + src);
break;
case 3:
str += mSP.getString("str", "");
Log.d(TAG, "STR:" + str);
break;
case 4:
to += mSP.getString("to", "");
Log.d(TAG, "TO:" + to);
break;
}
}
String name = mUserProfile.getName();
if (mUserProfile.getEmail() == null){
SharedPreferences mSharedPreferences = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
String email = mSharedPreferences.getString("email", null);
mDBHelper.createNewUser(name, 0, 0, email, soat, rtm, str, src, to);
} else {
String email = mUserProfile.getEmail();
mDBHelper.createNewUser(name, 0, 0, email, soat, rtm, str, src, to);
}
}
});
}
@Override
public void onFailure(AuthenticationException error) {
Form.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(Form.this, R.string.profile_request_failed, Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
FormAdapter
public class FormAdapter extends RecyclerView.Adapter<FormAdapter.ViewHolder> {
public static final String FORM_PREFERENCES = "FormPreferences";
private Context mContext;
private ArrayList<Document> mDocuments;
private ArrayList<Dates> list = new ArrayList<>();
public FormAdapter(Context mContext, ArrayList<Document> mDocuments) {
this.mContext = mContext;
this.mDocuments = mDocuments;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.format_form, parent, false));
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final Document document = mDocuments.get(position);
holder.mTitle.setText(document.getName());
holder.mTitleExpanded.setText(document.getName());
holder.mEditText.setTag(R.id.date_et, position);
holder.mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (int) holder.mEditText.getTag(R.id.date_et);
Log.d("pos", String.valueOf(position));
SharedPreferences mSP = v.getContext().getSharedPreferences(FORM_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor mEditor = mSP.edit();
String data = holder.mEditText.getText().toString();
switch (position){
case 0:
list.add(new Dates(0, data));
Log.d("SOAT", data);
mEditor.putString("soat", data);
mEditor.commit();
break;
case 1:
list.add(new Dates(1, data));
Log.d("RTM", data);
mEditor.putString("rtm", data);
mEditor.commit();
break;
case 2:
list.add(new Dates(2, data));
Log.d("SRC", data);
mEditor.putString("src", data);
mEditor.commit();
break;
case 3:
list.add(new Dates(3, data));
Log.d("STR", data);
mEditor.putString("str", data);
mEditor.commit();
break;
case 4:
list.add(new Dates(4, data));
Log.d("TO", data);
mEditor.putString("to", data);
mEditor.commit();
break;
}
}
});
}
@Override
public int getItemCount() {
return mDocuments != null ? mDocuments.size(): 0;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView mTitle, mTitleExpanded;
public Button mButton;
public EditText mEditText;
public ImageView mImageView;
private int originalHeight = 0;
private boolean isViewExpanded = false;
private ConstraintLayout mConstraintLayout;
private DateWatcher mDateWatcher;
public ViewHolder(View view) {
super(view);
view.setOnClickListener(this);
mConstraintLayout = (ConstraintLayout) view.findViewById(R.id.expanded);
mTitle = (TextView) view.findViewById(R.id.name_title_tv);
mTitleExpanded = (TextView) view.findViewById(R.id.name_title_tv_expanded);
mButton = (Button) view.findViewById(R.id.add_button);
mEditText = (EditText) view.findViewById(R.id.date_et);
mImageView = (ImageView) view.findViewById(R.id.imageView_up);
mDateWatcher = new DateWatcher(mEditText);
mEditText.addTextChangedListener(mDateWatcher);
}
在FormActivity
中,soat...
似乎没有初始化
private String soat, rtm, src, str, to;
并且日志尝试附加字符串:
soat += mSP.getString("soat", "");
在.gif中,我们可以看到soat, rtm, src, str, to
都是null
你能展示对字符串 soat, rtm, src, str, to
的其他操作吗?
我对调试的看法是:尝试给 soat, rtm, src, str, to
一些 "word",看看会发生什么。
嗨,
我有一个存储一些到期日期的应用程序,我正在尝试使用 SharedPreferences 将数据传递给另一个 activity。正如您在 .gif 中看到的那样,当按下 "Add" 按钮时它会正确记录数据,但是当我需要保存数据并在着陆时正确显示时页面,它将这个随机 null 添加到保存的数据中。
你知道这个空值被添加到哪里了吗?
如果您有任何反馈意见或需要任何其他相关代码,请随时关注我!
FormActivity
import static tech.destinum.recorderis.adapters.FormAdapter.FORM_PREFERENCES;
public class Form extends AppCompatActivity {
private UserProfile mUserProfile;
private DBHelper mDBHelper;
private Auth0 mAuth0;
private Context mContext;
private RecyclerView mRecyclerView;
private ArrayList<Document> mArrayList;
private FormAdapter mAdapter;
private String soat, rtm, src, str, to;
private static final String PREFERENCES = "Preferences";
private static final String TAG = "Form Activity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view_form);
mArrayList = new ArrayList<>();
mArrayList.add(new Document(R.string.doc_soat, 0));
mArrayList.add(new Document(R.string.doc_rtm, 1));
mArrayList.add(new Document(R.string.doc_src, 2));
mArrayList.add(new Document(R.string.doc_str, 3));
mArrayList.add(new Document(R.string.doc_tao, 4));
mAdapter = new FormAdapter(mContext, mArrayList);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.form_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.confirmation:
createUser();
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
break;
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
//TODO: New method for premium version
//free version
private void createUser() {
mAuth0 = new Auth0(getString(R.string.auth0_client_id), getString(R.string.auth0_domain));
// The process to reclaim an UserProfile is preceded by an Authentication call.
AuthenticationAPIClient aClient = new AuthenticationAPIClient(mAuth0);
aClient.tokenInfo(CredentialsManager.getCredentials(this).getIdToken())
.start(new BaseCallback<UserProfile, AuthenticationException>() {
@Override
public void onSuccess(final UserProfile payload) {
Form.this.runOnUiThread(new Runnable() {
public void run() {
mUserProfile = payload;
mDBHelper = new DBHelper(getApplicationContext());
SharedPreferences mSP = getSharedPreferences(FORM_PREFERENCES, Context.MODE_PRIVATE);
for (int i = 0; i < 6; i++){
switch (i){
case 0:
soat += mSP.getString("soat", "");
Log.d(TAG, "Soat:" + soat);
break;
case 1:
rtm += mSP.getString("rtm", "");
Log.d(TAG, "RTM:" + rtm);
break;
case 2:
src += mSP.getString("src", "");
Log.d(TAG, "SRC:" + src);
break;
case 3:
str += mSP.getString("str", "");
Log.d(TAG, "STR:" + str);
break;
case 4:
to += mSP.getString("to", "");
Log.d(TAG, "TO:" + to);
break;
}
}
String name = mUserProfile.getName();
if (mUserProfile.getEmail() == null){
SharedPreferences mSharedPreferences = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
String email = mSharedPreferences.getString("email", null);
mDBHelper.createNewUser(name, 0, 0, email, soat, rtm, str, src, to);
} else {
String email = mUserProfile.getEmail();
mDBHelper.createNewUser(name, 0, 0, email, soat, rtm, str, src, to);
}
}
});
}
@Override
public void onFailure(AuthenticationException error) {
Form.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(Form.this, R.string.profile_request_failed, Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
FormAdapter
public class FormAdapter extends RecyclerView.Adapter<FormAdapter.ViewHolder> {
public static final String FORM_PREFERENCES = "FormPreferences";
private Context mContext;
private ArrayList<Document> mDocuments;
private ArrayList<Dates> list = new ArrayList<>();
public FormAdapter(Context mContext, ArrayList<Document> mDocuments) {
this.mContext = mContext;
this.mDocuments = mDocuments;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.format_form, parent, false));
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final Document document = mDocuments.get(position);
holder.mTitle.setText(document.getName());
holder.mTitleExpanded.setText(document.getName());
holder.mEditText.setTag(R.id.date_et, position);
holder.mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (int) holder.mEditText.getTag(R.id.date_et);
Log.d("pos", String.valueOf(position));
SharedPreferences mSP = v.getContext().getSharedPreferences(FORM_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor mEditor = mSP.edit();
String data = holder.mEditText.getText().toString();
switch (position){
case 0:
list.add(new Dates(0, data));
Log.d("SOAT", data);
mEditor.putString("soat", data);
mEditor.commit();
break;
case 1:
list.add(new Dates(1, data));
Log.d("RTM", data);
mEditor.putString("rtm", data);
mEditor.commit();
break;
case 2:
list.add(new Dates(2, data));
Log.d("SRC", data);
mEditor.putString("src", data);
mEditor.commit();
break;
case 3:
list.add(new Dates(3, data));
Log.d("STR", data);
mEditor.putString("str", data);
mEditor.commit();
break;
case 4:
list.add(new Dates(4, data));
Log.d("TO", data);
mEditor.putString("to", data);
mEditor.commit();
break;
}
}
});
}
@Override
public int getItemCount() {
return mDocuments != null ? mDocuments.size(): 0;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView mTitle, mTitleExpanded;
public Button mButton;
public EditText mEditText;
public ImageView mImageView;
private int originalHeight = 0;
private boolean isViewExpanded = false;
private ConstraintLayout mConstraintLayout;
private DateWatcher mDateWatcher;
public ViewHolder(View view) {
super(view);
view.setOnClickListener(this);
mConstraintLayout = (ConstraintLayout) view.findViewById(R.id.expanded);
mTitle = (TextView) view.findViewById(R.id.name_title_tv);
mTitleExpanded = (TextView) view.findViewById(R.id.name_title_tv_expanded);
mButton = (Button) view.findViewById(R.id.add_button);
mEditText = (EditText) view.findViewById(R.id.date_et);
mImageView = (ImageView) view.findViewById(R.id.imageView_up);
mDateWatcher = new DateWatcher(mEditText);
mEditText.addTextChangedListener(mDateWatcher);
}
在FormActivity
中,soat...
似乎没有初始化
private String soat, rtm, src, str, to;
并且日志尝试附加字符串:
soat += mSP.getString("soat", "");
在.gif中,我们可以看到soat, rtm, src, str, to
都是null
你能展示对字符串 soat, rtm, src, str, to
的其他操作吗?
我对调试的看法是:尝试给 soat, rtm, src, str, to
一些 "word",看看会发生什么。