"Vista de contenido aún no creada" en fragmentos de Android

I am using Android Fragments with a viewpager setup. Basically there are 5 tabs you can swipe through.

4 of the 5 contain ListFragments that are populated by MySQL tables; this is done inside of an AsyncTask. When I swipe very fast I sometimes get this error:

09-01 07:54:01.243: E/AndroidRuntime(19706): FATAL EXCEPTION: main
09-01 07:54:01.243: E/AndroidRuntime(19706): java.lang.IllegalStateException: Content view not yet created
09-01 07:54:01.243: E/AndroidRuntime(19706):    at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
09-01 07:54:01.243: E/AndroidRuntime(19706):    at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
09-01 07:54:01.243: E/AndroidRuntime(19706):    at com.---.---.MasterCat$TopFrag$TopTask.onPostExecute(MasterCat.java:570)
09-01 07:54:01.243: E/AndroidRuntime(19706):    at com.---.---.MasterCat$TopFrag$TopTask.onPostExecute(MasterCat.java:1)
09-01 07:54:01.243: E/AndroidRuntime(19706):    at android.os.AsyncTask.finish(AsyncTask.java:631)
09-01 07:54:01.243: E/AndroidRuntime(19706):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-01 07:54:01.243: E/AndroidRuntime(19706):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
09-01 07:54:01.243: E/AndroidRuntime(19706):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 07:54:01.243: E/AndroidRuntime(19706):    at android.os.Looper.loop(Looper.java:137)
09-01 07:54:01.243: E/AndroidRuntime(19706):    at android.app.ActivityThread.main(ActivityThread.java:4928)
09-01 07:54:01.243: E/AndroidRuntime(19706):    at java.lang.reflect.Method.invokeNative(Native Method)
09-01 07:54:01.243: E/AndroidRuntime(19706):    at java.lang.reflect.Method.invoke(Method.java:511)
09-01 07:54:01.243: E/AndroidRuntime(19706):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-01 07:54:01.243: E/AndroidRuntime(19706):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-01 07:54:01.243: E/AndroidRuntime(19706):    at dalvik.system.NativeStart.main(Native Method)

I have intentionly not used a ProgressDialog because it makes swiping stutter quite a bit.

So I have two options and I need help either way:

  1. How to prevent this error and keep the list refreshing even while swiping.

  2. OR once the 5 fragments are loaded, turn off 'refresh while swiping'. In other words, the lists will stay the same unless you back out of the Activity and come back.

I'd prefer option #1. Please let me know what code you need to see:

    public void onCreate(Bundle savedInstanceState) {

        mSectionsPagerAdapter = new SectionsPagerAdapter(

        final ActionBar actionBar = getActionBar();

        mViewPager = (ViewPager) findViewById(R.id.viewpager);

                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    public void onPageSelected(int position) {


        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {


    public void onTabUnselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {


    public void onSaveInstanceState(Bundle savedInstanceState) {

    public void onRestoreInstanceState(Bundle savedInstanceState) {


    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

    public void onTabReselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {

        public Fragment getItem(int position) {
            Fragment f = null;
            switch (position) {
            case 0: {
                f = new MasterFrag();
                Bundle args = new Bundle();

            case 1: {
                f = new FeaturedFrag();
                Bundle args = new Bundle();

            case 4: {
                f = new TopFrag();
                Bundle args = new Bundle();

            case 3: {
                f = new NewFrag();
                Bundle args = new Bundle();

            case 2: {
                f = new TrendFrag();
                Bundle args = new Bundle();

                throw new IllegalArgumentException("not this many fragments: "
                        + position);

            return f;


Also, line 570 referenced in the LogCat pertains to a getListView() inside of an AsyncTask which is inside of a ListFragment not shown above.


Adding one of the 5 Fragments - the one referenced in LogCat - "TopFrag"

class TopTask extends AsyncTask<String, String, Void> {
        InputStream is = null;
        String result = "";

        protected Void doInBackground(String... params) {
            String url_select = "http://www.---.com/---/---.php";

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url_select);
            ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
            param.add(new BasicNameValuePair("Top", "Top"));

            try {
                httpPost.setEntity(new UrlEncodedFormEntity(param));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();

                // read content
                is = httpEntity.getContent();

            } catch (Exception e) {

                Log.e("log_tag", "Error in http connection " + e.toString());
            try {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();
                String line = "";
                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                result = sb.toString();

            } catch (Exception e) {
                // TODO: handle exception
                Log.e("log_tag", "Error converting result " + e.toString());

            return null;


        protected void onPostExecute(Void v) {

            String item, cat;
            try {
                JSONArray jArray = new JSONArray(result);
                JSONObject json_data = null;
                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);
                    item = json_data.getString("item");
                    cat = json_data.getString("category");


            } catch (JSONException e1) {
                Toast.makeText(getActivity(), "No Top Items Found",
            } catch (ParseException e1) {

            ListView listView;
            listView = getListView();

            listView.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long id) {
           // edit


            MasterCatObject[] mco = new MasterCatObject[items.size()];
            int index = 0;

            for (@SuppressWarnings("unused")
            String i : items) {
                mco[index] = new MasterCatObject(items.get(index),

            adapter = new AllTimeAdapter(getActivity(), mco);



preguntado el 01 de septiembre de 12 a las 16:09

2 Respuestas

at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
at com.---.---.MasterCat$TopFrag$TopTask.onPostExecute(MasterCat.java:570)

tu TopTask.onPostExecute() must not touch the UI while the fragment is not yet (before onCreateView) / no longer (after onDestroyView) shown. There is simply no ListView puedes obtener.

What you could do here is to update the datastructure that is used by the ListView so the next time the list is drawn the new data is included.

Respondido el 01 de Septiembre de 12 a las 16:09

Estoy recibiendo el ListView by the normal getListView() inside the PostExecute - would it be better to place it in the OnActivityCreated? - elmaestrodelaslechugas

OnActivityCreated should work but I would put it in onCreateView después de llamar super since that is definately a place where you can be sure that it is called when the ListView es creado. - zapl

Marking correct; I can already see things are much more stable. - elmaestrodelaslechugas

También el for loop you use to iterate over the items collection is a little counter intuitive. You know that items.get(index) == i, assuming a List, because that's how Java's enhanced for loops work. I see you also need to get the same index from the cats collection. In this case, you might use a Map<String, String> from item a category or a collection of classes (say MyPair) that have two values: item and category -- in that case you could iterate over your collection of MyPairs and get all the data. I'd recommend a Map though since they are exactly for key-value pairs (= - dcow

En mi onPostExecute() I only run my code for the listview if view is not null. Something like this: if(getView() != null && getActivity() != null) { /*set listview's adapter or whatever*/} - sufí

To avoid crash can check if ListFragment isAdded())

in the moment of exception ListFragment is detached.

respondido 15 mar '16, 19:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.