在Android App中设置和打开片段的问题

时间:2014-04-25 19:55:35

标签: java android android-fragments

我遇到了问题,因为我的应用程序崩溃而不是打开碎片。我有一个ListActivity,可以带你去另一个活动;在其他活动中,有两个片段。 ListActivity期望来自其中一个片段的结果。

我的代码在添加片段之前正在工作!然而,碎片不再出现,应用程序关闭......有没有人可能知道我的问题是什么?关于如何解决这个问题的任何建议?我衷心感谢所有和任何帮助,谢谢!我的代码如下。

ListActivity.java:

public class LyricList extends ListActivity {
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private LyricsDbAdapter mDbHelper;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lyriclist);
    mDbHelper = new LyricsDbAdapter(this);
    mDbHelper.open();
    fillData();
    registerForContextMenu(getListView());
}

private void fillData() {
    Cursor lyricsCursor = mDbHelper.fetchAllLyrics();
    startManagingCursor(lyricsCursor);
    String[] from = new String[]{LyricsDbAdapter.KEY_TITLE};
    int[] to = new int[]{R.id.text1};
    SimpleCursorAdapter lyrics = 
        new SimpleCursorAdapter(this, R.layout.lyrics_row, lyricsCursor, from, to);
    setListAdapter(lyrics);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(0, INSERT_ID, 0, R.string.menu_insert);
    return true;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getItemId()) {
        case INSERT_ID:
            createLyric();
            return true;
    }

    return super.onMenuItemSelected(featureId, item);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case DELETE_ID:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            mDbHelper.deleteLyric(info.id);
            fillData();
            return true;
    }
    return super.onContextItemSelected(item);
}

private void createLyric() {
    Intent i = new Intent(this, NextActivity.class);
    startActivityForResult(i, ACTIVITY_CREATE);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, NextActivity.class);
    i.putExtra(LyricsDbAdapter.KEY_ROWID, id);
    startActivityForResult(i, ACTIVITY_EDIT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    fillData();
}
}

应通过listActivity打开的另一个活动类:

public class NextActivity extends Activity {

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

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Tab one = actionBar.newTab().setText("Lyric Editor");
    Tab two = actionBar.newTab().setText("Loops");

    one.setTabListener(new MyTabListener(new LyricEditorFragment()));
    two.setTabListener(new MyTabListener(new LoopsFragment()));

    actionBar.addTab(one);
    actionBar.addTab(two);
}
public class MyTabListener implements TabListener{
    Fragment fragment;

    public MyTabListener(Fragment f){
        fragment = f;
    }
    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onTabSelected(Tab arg0, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        ft.replace(R.id.frame1, fragment);
    }
    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        ft.remove(fragment);
    }
}
    }

不确定是否要查看片段类,但这只是因为:

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

@Override
public void onCreate(Bundle savedInstanceState) {
    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();
        }

    });
}

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

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

    }

crashlog

1 个答案:

答案 0 :(得分:1)

问题是你从活动中获取了一个返回null的视图..你需要从onCreateView创建/膨胀视图..

点击此处,按照如何创建和使用片段视图的步骤进行操作。

Click Here