2014-04-25 46 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