Question

I have a class that should populate a listview. I used the same code into another activity (not a Fragment) and it works fine. In this Fragment, instead, it gives me an "IllegalStateException" ...
This is the log:

04-02 14:08:21.505: E/AndroidRuntime(25925): FATAL EXCEPTION: main
04-02 14:08:21.505: E/AndroidRuntime(25925): Process: com.social.bearv2, PID: 25925
04-02 14:08:21.505: E/AndroidRuntime(25925): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.social.bearv2/com.social.bearv2.PagerTabsActivity}: java.lang.IllegalStateException: Content view not yet created
04-02 14:08:21.505: E/AndroidRuntime(25925):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at android.app.ActivityThread.access$800(ActivityThread.java:145)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at android.os.Looper.loop(Looper.java:136)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at android.app.ActivityThread.main(ActivityThread.java:5081)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at java.lang.reflect.Method.invoke(Method.java:515)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at dalvik.system.NativeStart.main(Native Method)
04-02 14:08:21.505: E/AndroidRuntime(25925): Caused by: java.lang.IllegalStateException: Content view not yet created
04-02 14:08:21.505: E/AndroidRuntime(25925):    at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at com.social.bearv2.Page2Fragment.<init>(Page2Fragment.java:46)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at java.lang.Class.newInstanceImpl(Native Method)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at java.lang.Class.newInstance(Class.java:1208)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at com.social.bearv2.PagerTabsActivity.onCreate(PagerTabsActivity.java:36)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at android.app.Activity.performCreate(Activity.java:5231)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-02 14:08:21.505: E/AndroidRuntime(25925):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
04-02 14:08:21.505: E/AndroidRuntime(25925):    ... 12 more

And this is the class:

public class Page2Fragment extends ListFragment {
     List<NameValuePair> nameValuePairs;
     ArrayList<String> items = null;
     HttpClient httpclient;
     HttpPost httppost;
     HttpResponse response;
     HttpEntity httpentity;
     String is;
     private static SharedPreferences pref;
     private TextView testo;


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


        // Se container è vuoto restituisco null
        if (container == null) {
            return null;
        }
        // Altrimenti faccio l'inflate della view dal layout
        View view = (LinearLayout) inflater.inflate(R.layout.page2, container,
                false);

        Context ctx = getActivity();
          Resources res = ctx.getResources();
          String[] achievements = getResources().getStringArray(R.array.achievements);
          TypedArray immagini = res.obtainTypedArray(R.array.immagini);

          setListAdapter(new TestImmagineAdapter(ctx, R.layout.riga_lista, achievements, immagini));

          //setListAdapter(new ArrayAdapter<String>(this, R.layout.riga_lista, achievements));

          final ListView lv = getListView();
          lv.setTextFilterEnabled(true);
          testo = (TextView) view.findViewById(R.id.testo_vista);
          pref = getActivity().getSharedPreferences("MainActivity", 0);
          final SharedPreferences.Editor editor = pref.edit();
          if(pref.getInt("ach1", 0)== 1) {
              lv.getChildAt(0).setBackgroundColor(Color.WHITE);
          }
          lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
              // When clicked, show a toast with the TextView text
              //Toast.makeText(getApplicationContext(), testo.getText(),
                  //Toast.LENGTH_SHORT).show();
                int num = position;
                  Log.i("Elemento numero:", Integer.toString(num));   
            }
          });
        return view;
    }

}
Was it helpful?

Solution

  1. onCreateView should never return null.
  2. you should use onCreateView just in order to return the inflated View and using onViewCreated to look for elements of the inflated view. First parameter of onViewCreated is the View you inflated

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        return  inflater.inflate(R.layout.page2, container,
            false);
    }
    
    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) {
    
      Context ctx = getActivity();
      Resources res = ctx.getResources();
      String[] achievements = getResources().getStringArray(R.array.achievements);
      TypedArray immagini = res.obtainTypedArray(R.array.immagini);
    
      setListAdapter(new TestImmagineAdapter(ctx, R.layout.riga_lista, achievements, immagini));
    
    
      final ListView lv = getListView();
      lv.setTextFilterEnabled(true);
      testo = (TextView) view.findViewById(R.id.testo_vista);
      pref = getActivity().getSharedPreferences("MainActivity", 0);
      final SharedPreferences.Editor editor = pref.edit();
      if(pref.getInt("ach1", 0)== 1) {
          lv.getChildAt(0).setBackgroundColor(Color.WHITE);
      }
      lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
          // When clicked, show a toast with the TextView text
          //Toast.makeText(getApplicationContext(), testo.getText(),
              //Toast.LENGTH_SHORT).show();
            int num = position;
              Log.i("Elemento numero:", Integer.toString(num));   
        }
      });
    
    } 
    

OTHER TIPS

I'm just guessing but is it possible you have not called:

setContentView(...)

in the onCreate(...) method of your Activity?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top