Android-这究竟是为什么(QUOT;内容尚未创建"错误) [英] Android- Why is this happening ("Content not yet created" error)

查看:318
本文介绍了Android-这究竟是为什么(QUOT;内容尚未创建"错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续到这个问题:<一href=\"http://stackoverflow.com/questions/9286391/android-why-is-this-telling-me-content-view-not-yet-created\">Android - 这是为什么告诉我&QUOT;内容视图尚未创建&QUOT;?

This is a followup to this question: Android - why is this telling me "Content view not yet created"?

因此​​,下面code抛出一个错误:

So, the following code throws an error:

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;

     }

,但
如果我添加一个按钮的布局,并将code OnClick方法里面,它完美的作品。像这样的:

BUT, if i add a button to the layout and place the code inside the OnClick method, it works perfectly. Like this:

@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; 

为什么会出现这种情况。我明白它必须有一些做的片段生命周期,但什么?我怎样才能绕过这个所以列表列表视图填充片段打开时,不仅当我点击按钮?

Why does this happen. i understand it must have something to do with the fragments lifecycle, but what? How can I circumvent this so the list listview is populated when the fragment opens, and not only when i click the button?

感谢

编辑:这是主要的活动

 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>

推荐答案

这里的关键是,当setAdapter()被调用您的ListFragment内。

The key here is when setAdapter() is being called inside your ListFragment.

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

In your first example, setAdapter() was being called inside onCreateView() which won't work because the ListFragment checks to ensure there is a list defined in your layouts before allowing the adapter to be set and until onCreateView() returns the view you are creating, there isn't a layout to check. It's a chicken and egg problem.

第二个例子作品的原因是因为你没有真正callnig从内部onCreateView setAdapter()()。该适配器未设置直到按钮被点击你得到的布局。

The reason the second example works is because you aren't actually callnig setAdapter() from within onCreateView(). The adapter isn't set until the button is clicked in your resulting layout.

这里的基本问题是碎片从你不能做同样的各种各样的东西作为你是用来与你的活动做的,尤其是当它涉及到设置你的适配器。OnCreate()中的活动有所不同

The basic issue here is that fragments differ from activities in that you can't do the same sorts of things in onCreate() as you are used to doing with your activities, especially when it comes to setting up your adapters.

这篇关于Android-这究竟是为什么(QUOT;内容尚未创建&QUOT;错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆