0

我正在尝试构建一个基于基本的应用程序,该应用程序根据用户输入ListFragment从一个过渡到另一个。ListFragment

我使用ListViewAndroid 系统为 a 膨胀的默认值ListFragment,因此我不会覆盖onCreateView().

要设置 周围的边距ListFragment,我添加了一个GlobalLayoutListener.

现在,当我启动应用程序时,包含默认片段的第一个屏幕正确显示,并且边距设置正确。

但是,一旦我单击主布局中的图像,它会调用转换到具有相同onActivityCreated()方法的第二个片段,并且与第一个片段相同,当我尝试访问第二个片段时GlobalLayoutListener,我会在方法中遇到可怕的Content View not created yet错误.OnGlobalLayout()ListView

public void onActivityCreated(Bundle savedInstanceState){

     super.onActivityCreated(savedInstanceState);



     setListAdapter(new ArrayAdapter<String>(getActivity(),
                    R.layout.listcommon, R.id.label, MAIN_TITLES));




     ListView lv = getListView();

     lv.setDivider(null);
     lv.setDividerHeight(0);
     lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
     lv.setItemChecked(0, true);
     lv.setSelection(0);



     ViewTreeObserver observer = getListView().getViewTreeObserver();

     observer.addOnGlobalLayoutListener(fragmentLayoutListener);

}

ViewTreeObserver.OnGlobalLayoutListener fragmentLayoutListener = new   ViewTreeObserver.OnGlobalLayoutListener(){

    public void onGlobalLayout() {

    //Crashes here for second fragment   
    ListView listView = getListView();

    FrameLayout.LayoutParams params = (LayoutParams) listView.getLayoutParams();

    params.setMargins(10, 10, 10, 10);

    }

};

这是主要布局:(默认片段被添加FragmentTransaction.add()到 first FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent" 
android:layout_height="match_parent"
android:baselineAligned="false"
android:background="#00000000">


<FrameLayout
    android:id="@+id/TitlesContainer"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="40"
    android:layout_margin="10dip"
    android:background="@drawable/titlesbg"
    >


</FrameLayout>


<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="60"
    android:background="#00000000"
    >

<ImageView
    android:id="@+id/imageView_clickWheel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/clickImage" />

</FrameLayout>


</LinearLayout>

关于我应该怎么做以避免遇到此错误的任何想法?

onCreateView()毕竟我是否应该超越并膨胀自定义列表视图(但我没有这样的需要)?

编辑:

这是我将默认片段添加到主要活动的方法onCreate()

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    TitlesFragment fragment = (TitlesFragment) fragmentManager.findFragmentByTag(MAIN_SCREEN_TAG);
    if (fragment == null){
        fragment = TitlesFragment.newInstance(SCREEN_MAIN);
        fragmentTransaction.add(R.id.TitlesContainer, fragment, MAIN_SCREEN_TAG);

    } else {    
        fragmentTransaction.add(R.id.TitlesContainer, fragment);
    }

    fragmentTransaction.commit();

要执行转换,我会这样做:

    fragment = (TitlesFragment) fragmentManager.findFragmentByTag(SECOND_FRAGMENT_TAG);
                if (fragment == null){

                    fragment = TitlesFragment.newInstance(SECOND_FRAGMENT);
                    fragmentTransaction.replace(R.id.TitlesContainer, fragment, SECOND_FRAGMENT_TAG);
                } else {
                    fragmentTransaction.replace(R.id.TitlesContainer, fragment);
                }
    fragmentTransaction.commit();
4

2 回答 2

0

由于您没有提及它并且没有显示您的完整代码,我在这里猜测了一下,但以下可能是您的问题的原因。

我认为您需要先创建视图,然后才能访问它的元素。见这里这里

我的一个项目中的一个例子:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.leftlistfragment, container, false);
    return v;
}

编辑

也许问题是您创建片段的方式(我不熟悉您使用标签的方法)。也许这也对你有用:

        titleFrag = new TitlesFragment();
        getFragmentManager().beginTransaction()
                .replace(R.id.TitlesContainer, titleFrag, SECOND_FRAGMENT_TAG).commit();
于 2012-03-26T13:30:32.053 回答
0

我摆脱了 OnGlobalLayoutListener。相反,我在第一个 FrameLayout 上设置了“layout_padding”属性,以设置它所包含的 Fragment 的边距。

于 2012-03-27T17:40:56.983 回答