将随机空字符串值添加到对象的日志

时间:2017-03-28 03:02:48

标签: android android-recyclerview android-logcat

Gif

您好,

我有一个应用程序存储了一些过期日期,我正在尝试使用 SharedPreferences 将数据传递给另一个活动。正如您在 .gif 中看到的那样,当按下“添加”按钮时,它会正确记录数据,但是当我需要保存数据并在着陆页中正确显示时,它会随机添加已保存的数据 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);


    }

1 个答案:

答案 0 :(得分:1)

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上显示其他操作吗?

我对调试的看法是:尝试给出一些&#34; word&#34;到soat, rtm, src, str, to,看看发生了什么。