2014-04-25 29 views
0

我只是无法获得片段显示...!我知道这是我的onCreateView中的一个问题。有没有人可能会看到这个问题,并可以想出某种解决方案?Android - 显示片段的问题

第一片段实际上显示如果我注释掉线34-59,其与super.onCreate(savedInstanceState);onCreateView开始,和与闭合});confirmButton.setOnClickListener结束。但是,通过这些注释,单击其他片段的选项卡会导致应用程序崩溃。我也无法保存我想要从第一个片段检索的信息,因此我需要这些行。

我真诚地感谢所有和任何帮助,非常感谢您的时间!

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); 
    } 
} 
} 

这里是我的logcat的照片:

logcat

+0

什么是第38行? – Aashir

+0

mTitleText =(EditText)getView()。findViewById(R.id.title); – webhoodlum

回答

2

的getView()您使用将返回null作为该片段尚未有其视图设置尚未功能(它是什么你实际上一旦你返回视图)。相反,您需要使用view.findViewById()并搜索刚刚膨胀的视图。

+0

它的工作很精彩.....很多很多谢谢! :') – webhoodlum