如何在ViewPager中更改保存的数据? (当前为空,对象错误,但查看保存的数据时不存在)

时间:2018-09-06 19:05:57

标签: android android-fragments android-edittext android-viewpager save

简介

大家好,我正在开发一个问题表单应用,但很长一段时间以来,我仍然遇到相同的错误。我已经检查了很多与此主题有关的StackOverFlow问题,但是找不到适用于我的问题的解决方案。

问题

添加表单后,将出现6个viewpager幻灯片,每个幻灯片包含一个问题(EditText)。填写问题后,将保存项目。如果然后打开表单,则可以查看所有保存在这些EditText中的数据。不幸的是,尝试更改这些值会导致片段中我的String getTitle内的getText处的getText出现空对象引用错误(Frag1和Frag2除外)。

问题

我不知道出了什么问题,保存没有问题,查看保存的数据也没有问题。但是更改除Frag1或Frag2以外的任何其他片段都可以解决有关String getTitle的空对象错误……希望有人知道出了什么问题。也许与保存的数据没有被转换为新数据有关,或者其他原因?

代码

public class Frag4 extends Fragment {
private static final String EXTRA_TEXT = "text";
private EditText mEtVraag4;

public static Frag4 newInstance(String message) {
    Bundle args = new Bundle();
    args.putString(EXTRA_TEXT, message);
    Frag4 fragment = new Frag4();
    fragment.setArguments(args);
    return fragment;
}
public Frag4 () {

}

public String getTitle(){
    return mEtVraag4.getText().toString();
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.frag4_layout, container, false);
    mEtVraag4 = (EditText) view.findViewById(R.id.note_et_title4);
    Bundle bundle = getArguments();
    if (bundle != null) {
        mEtVraag4.setText(bundle.getString(EXTRA_TEXT));
    }
    return view;
}


我活动中的相关部分

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    myDialog1 = new Dialog(this);

    String mtitle = "";
    String mquestion2 = "";

    String mquestion4 = "";
    String mquestion5 = "";
    String mquestion6 = "";
    String mquestion7 = "";

    mNoteFileName = getIntent().getStringExtra("NOTE_FILE");
    if(mNoteFileName !=null && !mNoteFileName.isEmpty()) {
        mLoadedNote = Utilities.getNoteByName(this, mNoteFileName);
        if(mLoadedNote !=null) {
            mtitle = mLoadedNote.getTitle();
            mquestion2 = mLoadedNote.getVraag2();

            mquestion4 = mLoadedNote.getVraag4();
            mquestion5 = mLoadedNote.getVraag5();
            mquestion6 = mLoadedNote.getVraag6();
            mquestion7 = mLoadedNote.getVraag7();

        }
    }

    List<Fragment> fragments = new ArrayList<>();
    fragments.add(Frag1.newInstance(mtitle));
    fragments.add(Frag2.newInstance(mquestion2));

    fragments.add(Frag4.newInstance(mquestion4));
    fragments.add(Frag5.newInstance(mquestion5));
    fragments.add(Frag6.newInstance(mquestion6));
    fragments.add(Frag7.newInstance(mquestion7));

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),fragments);

    mViewPager = (ViewPager) findViewById(R.id.container);

    mViewPager.setAdapter(mSectionsPagerAdapter);

}


 private void saveNote() {

    String title1="",title2="", title4="",title5="",title6="", title7="";
    for(int position = 0; position<mSectionsPagerAdapter.getCount();position++){
        Fragment fragment = mSectionsPagerAdapter.getItem(position);
        if(fragment instanceof Frag1){
            title1 = ((Frag1)fragment).getTitle();
        } else  if(fragment instanceof Frag2){
            title2 = ((Frag2)fragment).getTitle();

        } else  if(fragment instanceof Frag4){
            title4 = ((Frag4)fragment).getTitle();
        } else  if(fragment instanceof Frag5){
            title5 = ((Frag5)fragment).getTitle();
        } else  if(fragment instanceof Frag6){
            title6 = ((Frag6)fragment).getTitle();
        } else  if(fragment instanceof Frag7){
            title7 = ((Frag7)fragment).getTitle();
        }
    }

    boolean valid = !TextUtils.isEmpty(title1);


    Ideas note;

    if(!valid){
        Toast.makeText(this, "Please provide inputs", Toast.LENGTH_SHORT).show();
        return;
    }
    note = new Ideas((mLoadedNote != null)?mLoadedNote.getDateTime():System.currentTimeMillis(), title1, title2, title4, title5, title6, title7);

    if (Utilities.saveNote(this, note)){
        Toast.makeText(this, "saved", Toast.LENGTH_SHORT).show();
    } else{
        Toast.makeText(this, "not enough space", Toast.LENGTH_SHORT).show();
    }

    finish();
}

0 个答案:

没有答案