Question

I just simply cannot get the fragments to display...! and I know it's a problem within my onCreateView. Does anyone possibly see the issue and can think of some sort of solution??

The first fragment actually displays if I comment out lines 34-59, which start with the super.onCreate(savedInstanceState); with the onCreateView, and ends with the closing }); for the confirmButton.setOnClickListener. However, with those lines commented out, the tab for the other fragment, when clicked, leads to the app crashing. I also can't save the information that I want to retrieve from the first fragment by doing this... so I do need those lines.

I sincerely appreciate all and any help, thank you so much for your time!!

public class LyricEditorFragment extends Fragment {
private EditText mTitleText;
private EditText mBodyText;
private Long mRowId;
private LyricsDbAdapter mDbHelper;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.activity_lyriceditor, container, false);
    super.onCreate(savedInstanceState);
    mDbHelper = new LyricsDbAdapter(getActivity());
    mDbHelper.open();

    mTitleText = (EditText) getView().findViewById(R.id.title);
    mBodyText = (EditText) getView().findViewById(R.id.body);

    Button confirmButton = (Button) getView().findViewById(R.id.confirm);

    mRowId = (savedInstanceState == null) ? null :
        (Long) savedInstanceState.getSerializable(LyricsDbAdapter.KEY_ROWID);
    if (mRowId == null) {
        Bundle extras = getActivity().getIntent().getExtras();
        mRowId = extras != null ? extras.getLong(LyricsDbAdapter.KEY_ROWID)
                                : null;
    }

    populateFields();

    confirmButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            getActivity().setResult(Activity.RESULT_OK);
            getActivity().finish();
        }
    });
    return view;
}

private void populateFields() {
    if (mRowId != null) {
        Cursor lyric = mDbHelper.fetchLyric(mRowId);
        getActivity().startManagingCursor(lyric);
        mTitleText.setText(lyric.getString(
                lyric.getColumnIndexOrThrow(LyricsDbAdapter.KEY_TITLE)));
        mBodyText.setText(lyric.getString(
                lyric.getColumnIndexOrThrow(LyricsDbAdapter.KEY_BODY)));
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    saveState();
    outState.putSerializable(LyricsDbAdapter.KEY_ROWID, mRowId);
}

@Override
public void onPause() {
    super.onPause();
    saveState();
}

@Override
public void onResume() {
    super.onResume();
    populateFields();
}

private void saveState() {
    String title = mTitleText.getText().toString();
    String body = mBodyText.getText().toString();

    if (mRowId == null) {
        long id = mDbHelper.createLyric(title, body);
        if (id > 0) {
            mRowId = id;
        }
    } else {
        mDbHelper.updateLyric(mRowId, title, body);
    }
}
}

Here is a picture of my logcat:

logcat

Was it helpful?

Solution

The getView() Function your using will be returning null as the fragment hasn't has its view set yet (Its what your actually doing once you return the view). Instead you need to use view.findViewById() and search the view you just inflated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top