为什么还没有添加listfragment

时间:2014-06-13 12:23:45

标签: android android-fragments android-listfragment android-lifecycle android-actionbar-compat

我有一个列表片段,其中包含其布局,TextView和列表(ID为@android:id/list)。在onCreate of activity中,我将片段添加到容器

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Log.e(TAG, "Inside OnCreate");
    super.onCreate(savedInstanceState);
    showPdIndeterminate();
    ... /* registering some receivers */  ...
    mInstance = this;
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    setContentView(R.layout.activity_main);
    myListFragment = getSupportFragmentManager().findFragmentById(
            R.id.container);
    if (myListFragment == null) {
            Log.e(TAG,"Creating new MyListFragment");
            myList = new MyListFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, myList).commit();
        }

在MyListFragments' onActivityCreated我调用一个函数来改变布局中的textview

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getActivity().sendBroadcast(new Intent(MainActivity.LIST_INIT));
        selfInterface.onTimeToChangeTitle("Fuzzy Logic Details");

    }

功能是

@Override
public void onTimeToChangeTitle(String title) {
    if (myListFragment != null) {
        // Log.e(TAG, "Inside onTimeToChangeTitle");
        if (myListFragment.isAdded()) {
            Log.e(TAG, "MyListFragment added");
        } else {
            Log.e(TAG, "MyListFragment yet to be  added");
            getSupportFragmentManager().beginTransaction().attach(myListFragment).commit();
        }
    }else{
        Log.e(TAG, "MyListFragment itself null");
    }
    TextView t = (TextView) findViewById(R.id.listTitle);
    if (t != null)
        t.setText(title);
    else
        Log.e(TAG, "onTimeToChangeTitle: textview null");

}

方向更改后,我看到日志为MyListFragment yet to be added,因此我尝试了getSupportFragmentManager().beginTransaction().attach(myListFragment).commit();,但这给了cannot perform this operation here... exception

我的问题是片段尚未添加,标题不会更改,在这种情况下,如果我尝试getListView(),则会抛出list content not available exception

如何确保列表的视图可用并添加,以便更改其内容成功?

注1 :我也看到MyListFragment yet to be added也是在重新启动后按启动器图标重新启动应用程序。

注意2 :我已将setRetainState(true)设置为片段。

0 个答案:

没有答案