java.lang.IllegalStateException:使用抽屉时尚未创建的内容视图

时间:2014-12-08 07:24:08

标签: java android android-fragments

我使用抽屉列表为我的Android应用程序。 有问题。 当我随机点击(非常快)然后我得到forcestop并在Log中显示java.lang.IllegalStateException: Content view not yet created。 我正在使用片段

我还遇到以下运行时错误 在android.support.v4.app.ListFragment.ensureList(ListFragment.java:32

从抽屉列表中选择

,这是我的代码的部分
Fragment home = new Home();
    Fragment feeds = new Feeds_ListView(); 

.....

private void selectItem(int position) 
{

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    switch (position) 
    {

        case 0:
            setTitle(title[position]);
            ft.replace(R.id.content_frame, feeds);

            break;
        case 1:
            setTitle(title[position]);
            ft.replace(R.id.content_frame, NewPostFragment);
            break;
        case 2:
            setTitle(title[position]);
            ft.replace(R.id.content_frame, SearchDetailsFragment);
            break;
        case 3:
            setTitle(title[position]);
            ft.replace(R.id.content_frame, feeds1);
            break;
        case 4:
            setTitle(title[position]);
            ft.replace(R.id.content_frame, UserDetailsFragment);
            break;
        case 5:
            setTitle(title[position]);
            ft.replace(R.id.content_frame, FBActivity);
            break;
    }
    ft.commit();
    mDrawerList.setItemChecked(position, true);
    setTitle(title[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}


@Override
protected void onPostCreate(Bundle savedInstanceState) 
{
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public void setTitle(CharSequence title) 
{
    mTitle = title;
    getSupportActionBar().setTitle(mTitle);
}

这是Feeds_ListView();

的一部分
    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{   
    context=container.getContext();
    rootView = inflater.inflate(R.layout.feeds_listview_layout, container, false);


    list = (ListView) rootView.findViewById(android.R.id.list);
    mRelativeLayout=rootView.findViewById(R.id.mRelativeLayout);

    saveProgress =(ProgressBar) rootView.findViewById(R.id.loadpost);
    saveProgress.setVisibility(View.INVISIBLE);



    userAdapter = new CustomListAdapter(getActivity(), R.layout.feeds_listview_item,userArray);





    mRelativeLayout.setVisibility(View.GONE);

    list.setItemsCanFocus(false);
    list.setAdapter(userAdapter);
    return rootView;

}





@Override
public void onActivityCreated(Bundle savedInstanceState)
{ 
    super.onActivityCreated(savedInstanceState);

     SharedPreferences settings =getActivity().getSharedPreferences("newdatabase",0);

        String user_id=settings.getString("user_id", "--");

        if(user_id.equals("--") || user_id.equals(""))
        {   FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();          

            ft.replace(R.id.content_frame, FBActivity);
            ft.commit();
        }else{
                 new Onscrollasync().execute();

            ((PullToRefresh_Master) getListView()).setOnRefreshListener(new OnRefreshListener() 
            {
                @Override
                public void onRefresh() 
                {
                    new Onscrollasync().execute();
                }
            });
        }


}

2 个答案:

答案 0 :(得分:0)

问题在于您在Home中编写的片段onCreateView中的代码。将onCreateView()中的代码移至onActivityCreated()onViewCreated()。 StackOverlfow上有几个类似的问题,如here

答案 1 :(得分:0)

尝试此操作而不是给容器充气,尝试在没有容器的情况下给视图充气,即

rootView = inflater.inflate(R.layout.feeds_listview_layout,null);

让我知道这是否有效。