Android-为什么会发生这种情况(“尚未创建内容”错误)

时间:2012-02-15 16:25:33

标签: android android-fragments android-lifecycle

这是此问题的后续内容:Android - why is this telling me "Content view not yet created"?

因此,以下代码会引发错误:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
       Bundle savedInstanceState) {

        LinearLayout Layout5 = (LinearLayout) inflater.inflate(R.layout.tab_frag5_layout, container, false);

        Cursor allBands;
        MyDatabase db;

        Context ctx = (Context)TabFragment5.this.getActivity();


        db = new MyDatabase(ctx);
        allBands = db.getBands();


        ListAdapter adapter = new SimpleCursorAdapter (ctx, 
                R.layout.listelement, 
                allBands, 
                new String[] {"BandName"},  
                new int[] {R.id.text15});

        getListView().setAdapter(adapter);  

        return Layout5;

     }

,但 如果我在布局中添加一个按钮并将代码放在OnClick方法中,它就可以完美地工作。像这样:

@Override
         public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState) {

            LinearLayout Layout5 = (LinearLayout) inflater.inflate(R.layout.tab_frag5_layout, container, false);
            // Register for the Button.OnClick event 
            Button bShop = (Button)Layout5.findViewById(R.id.button_test);
            bShop.setOnClickListener(new View.OnClickListener() {           
                @Override
                public void onClick(View v) {

                    Cursor allBands;
                    MyDatabase db;

                    Context ctx = (Context)TabFragment5.this.getActivity();


                    db = new MyDatabase(ctx);
                    allBands = db.getBands();


                    ListAdapter adapter = new SimpleCursorAdapter (ctx, 
                            R.layout.listelement, 
                            allBands, 
                            new String[] {"BandName"},  
                            new int[] {R.id.text15});   

                    getListView().setAdapter(adapter);

                        }
                    });                                                                 

            return Layout5; 

为什么会这样。我知道它必须与片段生命周期有关,但是什么?我怎样才能避免这种情况,以便在片段打开时填充列表列表视图,而不仅仅是当我点击按钮时?

由于

编辑:这是主要活动

 public class TabsFragmentActivity extends FragmentActivity implements TabHost.OnTabChangeListener {

private TabHost mTabHost;
private HashMap mapTabInfo = new HashMap();
private TabInfo mLastTab = null;



private class TabInfo {
     private String tag;
     private Class clss;
     private Bundle args;
     private Fragment fragment;
     TabInfo(String tag, Class clazz, Bundle args) {
         this.tag = tag;
         this.clss = clazz;
         this.args = args;
     }

}

class TabFactory implements TabContentFactory {

    private final Context mContext;

    /**
     * @param context
     */
    public TabFactory(Context context) {
        mContext = context;
    }


    /** (non-Javadoc)
     * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
     */
    public View createTabContent(String tag) {
        View v = new View(mContext);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }

}
/** (non-Javadoc)
 * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
 */
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Step 1: Inflate layout
    setContentView(R.layout.tabs_layout);
    // Step 2: Setup TabHost
    initialiseTabHost(savedInstanceState);



    if (savedInstanceState != null) {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab     as per the saved state
    }
}

  /** (non-Javadoc)
 * @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle)
 */
   protected void onSaveInstanceState(Bundle outState) {
    outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
    super.onSaveInstanceState(outState);
}

/**
 * Step 2: Setup TabHost
 */
private void initialiseTabHost(Bundle args) {
    mTabHost = (TabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup();
    TabInfo tabInfo = null;
    TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("s1"), ( tabInfo = new TabInfo("Tab1", TabFragment1.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("s2"), ( tabInfo = new TabInfo("Tab2", TabFragment2.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("s3"), ( tabInfo = new TabInfo("Tab3", TabFragment3.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab4").setIndicator("s4"), ( tabInfo = new TabInfo("Tab4", TabFragment4.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab5").setIndicator("s5"), ( tabInfo = new TabInfo("Tab5", TabFragment5.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab6").setIndicator("s6"), ( tabInfo = new TabInfo("Tab6", TabFragment6.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    // Default to first tab
    this.onTabChanged("Tab1");
    //
    mTabHost.setOnTabChangedListener(this);
}

/**
 * @param activity
 * @param tabHost
 * @param tabSpec
 * @param clss
 * @param args
 */
private static void addTab(TabsFragmentActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
    // Attach a Tab view factory to the spec
    tabSpec.setContent(activity.new TabFactory(activity));
    String tag = tabSpec.getTag();

    // Check to see if we already have a fragment for this tab, probably
    // from a previously saved state.  If so, deactivate it, because our
    // initial state is that a tab isn't shown.
    tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
    if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
        FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
        ft.detach(tabInfo.fragment);
        ft.commit();
        activity.getSupportFragmentManager().executePendingTransactions();
    }

    tabHost.addTab(tabSpec);
}

/** (non-Javadoc)
9
 * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
 */
public void onTabChanged(String tag) {
    TabInfo newTab = (TabInfo) this.mapTabInfo.get(tag);
   if (mLastTab != newTab) {
        FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
        if (mLastTab != null) {
            if (mLastTab.fragment != null) {

                ft.detach(mLastTab.fragment);
            }

       }
        if (newTab != null) {
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(this,
                        newTab.clss.getName(), newTab.args);
                ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
            } else {
                ft.attach(newTab.fragment);

            }
        }

        mLastTab = newTab;
        ft.commit();
        this.getSupportFragmentManager().executePendingTransactions();
    }
}

}

片段XML

        <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/mylayout2"
  android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#D6D6D6"
 >

<Button
    android:id="@+id/button_test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:text="Button" />

<ListView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="0" 
    android:id="@android:id/list"></ListView>

2 个答案:

答案 0 :(得分:4)

这里的关键是在ListFragment中调用setAdapter()时。

在你的第一个例子中,setAdapter()在onCreateView()内被调用,因为ListFragment检查以确保在允许设置适配器之前在布局中定义了一个列表,直到onCreateView( )返回您正在创建的视图,没有要检查的布局。这是鸡和蛋的问题。

第二个例子起作用的原因是因为你实际上并没有从onCreateView()中调用setAdapter()。在生成的布局中单击按钮之前,不会设置适配器。

这里的基本问题是片段与活动的不同之处在于,您不能像处理活动那样在onCreate()中执行相同类型的操作,尤其是在设置适配器时

答案 1 :(得分:1)

您是否扩展了Fragment Class或ListFragment? 这就是我在ListFragment中使用它的方法 - &gt;

public class FooFragment extends ListFragment{

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_eventlist, null);
        eventAdapter = new EventAdapter(getActivity(), R.layout.list_item_event, events);
        setListAdapter(eventAdapter);
        return root;
        }
}

编辑:

只需修改变量和类型:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View root = inflater.inflate(R.layout.fragment_activitystream, null);
            return root;
            }

     @Override
     public void onCreate(Bundle savedInstanceState){
         super.onCreate(savedInstanceState);
         states = new ArrayList<Status>();
             ...
         YourAdapter asa = new YourAdapter(getActivity(), R.layout.list_item_status, states);
         setListAdapter(asa);
         Log.i(TAG, "onCreate is done");
     }