llegalStateException异常在ListFragm

llegalStateException异常在ListFragm

本文介绍了IllegalStateException异常在ListFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该填充列表视图的类。我用同样的code到另一个活动(不是片段),它工作正常。在这个片段,相反,它给了我一个IllegalStateException异常......结果这是日志:

  04-02 14:08:21.505:E / AndroidRuntime(25925):致命异常:主要
04-02 14:08:21.505:E / AndroidRuntime(25925):工艺:com.social.bearv2,PID:25925
04-02 14:08:21.505:E / AndroidRuntime(25925):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.social.bearv2 / com.social.bearv2.PagerTabsActivity}:java.lang.IllegalStateException:内容查看尚未创建
04-02 14:08:21.505:E / AndroidRuntime(25925):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
04-02 14:08:21.505:E / AndroidRuntime(25925):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
04-02 14:08:21.505:E / AndroidRuntime(25925):在android.app.ActivityThread.access $ 800(ActivityThread.java:145)
04-02 14:08:21.505:E / AndroidRuntime(25925):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1206)
04-02 14:08:21.505:E / AndroidRuntime(25925):在android.os.Handler.dispatchMessage(Handler.java:102)
04-02 14:08:21.505:E / AndroidRuntime(25925):在android.os.Looper.loop(Looper.java:136)
04-02 14:08:21.505:E / AndroidRuntime(25925):在android.app.ActivityThread.main(ActivityThread.java:5081)
04-02 14:08:21.505:E / AndroidRuntime(25925):在java.lang.reflect.Method.invokeNative(本机方法)
04-02 14:08:21.505:E / AndroidRuntime(25925):在java.lang.reflect.Method.invoke(Method.java:515)
04-02 14:08:21.505:E / AndroidRuntime(25925):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:791)
04-02 14:08:21.505:E / AndroidRuntime(25925):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
04-02 14:08:21.505:E / AndroidRuntime(25925):在de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
04-02 14:08:21.505:E / AndroidRuntime(25925):在dalvik.system.NativeStart.main(本机方法)
04-02 14:08:21.505:E / AndroidRuntime(25925):java.lang.IllegalStateException:通过引起内容视图尚未创建
04-02 14:08:21.505:E / AndroidRuntime(25925):在android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
04-02 14:08:21.505:E / AndroidRuntime(25925):在android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
。04-02 14:08:21.505:E / AndroidRuntime(25925):在com.social.bearv2.Page2Fragment<&初始化GT;(Page2Fragment.java:46)
04-02 14:08:21.505:E / AndroidRuntime(25925):在java.lang.Class.newInstanceImpl(本机方法)
04-02 14:08:21.505:E / AndroidRuntime(25925):在java.lang.Class.newInstance(Class.java:1208)
04-02 14:08:21.505:E / AndroidRuntime(25925):在android.support.v4.app.Fragment.instantiate(Fragment.java:402)
04-02 14:08:21.505:E / AndroidRuntime(25925):在android.support.v4.app.Fragment.instantiate(Fragment.java:377)
04-02 14:08:21.505:E / AndroidRuntime(25925):在com.social.bearv2.PagerTabsActivity.onCreate(PagerTabsActivity.java:36)
04-02 14:08:21.505:E / AndroidRuntime(25925):在android.app.Activity.performCreate(Activity.java:5231)
04-02 14:08:21.505:E / AndroidRuntime(25925):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-02 14:08:21.505:E / AndroidRuntime(25925):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
04-02 14:08:21.505:E / AndroidRuntime(25925):... 12多

这是类:

 公共类Page2Fragment扩展ListFragment {
     清单<&的NameValuePair GT; namevaluepairs中;
     ArrayList的<串GT;项=无效;
     HttpClient的HttpClient的;
     HttpPost httppost;
     HTT presponse响应;
     HttpEntity httpentity;
     字符串是;
     私人静态共享preferences preF;
     私人TextView的德图;
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){
        //硒容器èvuoto restituisco空
        如果(集装箱== NULL){
            返回null;
        }
        // Altrimenti法西奥L'充气德拉观点DAL布局
        查看查看=(的LinearLayout)inflater.inflate(R.layout.page2,集装箱,
                假);        上下文CTX = getActivity();
          资源解析度= ctx.getResources();
          的String [] =成就getResources()getStringArray(R.array.achievements)。
          TypedArray immagini = res.obtainTypedArray(R.array.immagini);          setListAdapter(新TestImmagineAdapter(CTX,R.layout.riga_lista,成就,immagini));          // setListAdapter(新ArrayAdapter<串GT;(这一点,R.layout.riga_lista,成就));          最终的ListView LV = getListView();
          lv.setTextFilterEnabled(真);
          德图=(TextView中)view.findViewById(R.id.testo_vista);
          。preF = getActivity()getShared preferences(MainActivity,0);
          最后一个共享preferences.Editor编辑器= pref.edit();
          如果(pref.getInt(ACH1,0)== 1){
              lv.getChildAt(0).setBackgroundColor(Color.WHITE);
          }
          lv.setOnItemClickListener(新OnItemClickListener(){
            公共无效onItemClick(适配器视图<>母公司,观景,
                INT位置,长的id){
              //单击时,显示的文字TextView的敬酒
              //Toast.makeText(getApplicationContext(),testo.getText(),
                  //Toast.LENGTH_SHORT).show();
                INT NUM =位置;
                  Log.i(Elemento的NUMERO:Integer.toString(NUM));
            }
          });
        返回视图。
    }}


解决方案

  1. onCreateView 应该不会返回

  2. 你应该使用 onCreateView 只是为了回报充气查看,并使用 onViewCreated 寻找充气视图元素。 onViewCreated 的第一个参数是查看您虚增

     公开查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
        返回inflater.inflate(R.layout.page2,集装箱,
            假);
    }@覆盖
    公共无效onViewCreated(查看视图,捆绑savedInstanceState){  上下文CTX = getActivity();
      资源解析度= ctx.getResources();
      的String [] =成就getResources()getStringArray(R.array.achievements)。
      TypedArray immagini = res.obtainTypedArray(R.array.immagini);  setListAdapter(新TestImmagineAdapter(CTX,R.layout.riga_lista,成就,immagini));
      最终的ListView LV = getListView();
      lv.setTextFilterEnabled(真);
      德图=(TextView中)view.findViewById(R.id.testo_vista);
      。preF = getActivity()getShared preferences(MainActivity,0);
      最后一个共享preferences.Editor编辑器= pref.edit();
      如果(pref.getInt(ACH1,0)== 1){
          lv.getChildAt(0).setBackgroundColor(Color.WHITE);
      }
      lv.setOnItemClickListener(新OnItemClickListener(){
        公共无效onItemClick(适配器视图<>母公司,观景,
            INT位置,长的id){
          //单击时,显示的文字TextView的敬酒
          //Toast.makeText(getApplicationContext(),testo.getText(),
              //Toast.LENGTH_SHORT).show();
            INT NUM =位置;
              Log.i(Elemento的NUMERO:Integer.toString(NUM));
        }
      });}


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

}
解决方案
  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));
        }
      });
    
    }
    

这篇关于IllegalStateException异常在ListFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 18:43