Listview onitemclick in Fragment

时间:2014-07-13 14:31:45

标签: android listview android-listview fragment onitemclicklistener

我想列出具有可点击的新活动的视图。当我添加onitemclick程序时停止了。 Logcat说:

07-13 09:09:18.873:E / AndroidRuntime(1790):java.lang.IllegalStateException:尚未创建的内容视图

在onitemclick listview工作之前。我创建所有xml文件并在清单文件中添加强化。我该如何解决这个问题?

这是我的主要java代码:

public class FragmentThree extends ListFragment {


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

        setListAdapter(new MyAdapter(getActivity(), android.R.layout.simple_list_item_1, R.id.mech ,getResources().getStringArray(R.array.parts)));


        ListView listView = getListView();
        listView.setTextFilterEnabled(true);

        listView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Intent intent = null;
                switch(position) {
                case 0:
                        intent = new Intent(getActivity(), FragmentThreeOne.class);
                        startActivity(intent);
                break;
                case 1:
                       intent = new Intent(getActivity(), FragmentThreeTwo.class);
                       startActivity(intent);
                       break;

              case 2:
                   intent = new Intent(getActivity(), FragmentThreeThree.class);
                   startActivity(intent);
                   break;

            case 3:
                   intent = new Intent(getActivity(), FragmentThreeFour.class);
                   startActivity(intent);
                   break;

            case 4:
                   intent = new Intent(getActivity(), FragmentThreeFive.class);
                   startActivity(intent);
                   break;

            case 5:
                   intent = new Intent(getActivity(), FragmentThreeSix.class);
                   startActivity(intent);
                   break;

            case 6:
                   intent = new Intent(getActivity(), FragmentThreeSeven.class);
                   startActivity(intent);
                   break;

            case 7:
                   intent = new Intent(getActivity(), FragmentThreeEight.class);
                   startActivity(intent);
                   break;

            case 8:
                   intent = new Intent(getActivity(), FragmentThreeNine.class);
                   startActivity(intent);
                   break;

            case 9:
                   intent = new Intent(getActivity(), FragmentThreeTen.class);
                   startActivity(intent);
                   break;

            case 10:
                   intent = new Intent(getActivity(), FragmentThreeEleven.class);
                   startActivity(intent);
                   break;



                default:
                }

            }
        });


          View rootView = inflater .inflate(R.layout.fragment_three, container, false);  
          return rootView;
 }

    private class MyAdapter extends ArrayAdapter<String> {

        public MyAdapter(Context context, int resource,
                int textViewResourceId, String[] strings) {
            super(context, resource, textViewResourceId, strings);
            // TODO Auto-generated constructor stub
        }





        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            LayoutInflater inflater =(LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View row = inflater.inflate(R.layout.list_item_one, parent, false);
            String[] items = getResources().getStringArray(R.array.parts);

            ImageView iv = (ImageView) row.findViewById(R.id.mechres);

            TextView tv = (TextView) row.findViewById(R.id.mech);

            tv.setText(items[position]);

            if(items[position].equals("itemone")){

                iv.setImageResource(R.drawable.one);
            }

            else if (items[position].equals("itemtwo")){

                iv.setImageResource(R.drawable.twp);
            }
            else if (items[position].equals("itemthree")){

                                iv.setImageResource(R.drawable.three);
                            }

            else if (items[position].equals("itemfour")){

                    iv.setImageResource(R.drawable.four);
                }
            else if (items[position].equals("itemfive")){

                    iv.setImageResource(R.drawable.five);
                }

            else if (items[position].equals("itemsix")){

                    iv.setImageResource(R.drawable.six);
                }

            else if (items[position].equals("itemseven")){

                    iv.setImageResource(R.drawable.seven);
                }

            else if (items[position].equals("itemeight")){

                iv.setImageResource(R.drawable.eight);
            }
            else if (items[position].equals("itemnine")){

                iv.setImageResource(R.drawable.nine);
            }
            else if (items[position].equals("itemten")){

                iv.setImageResource(R.drawable.ten);
            }
            else if (items[position].equals("itemeleven")){

                iv.setImageResource(R.drawable.eleven);
            }



            return row;
        }

    }

        }

1 个答案:

答案 0 :(得分:0)

将所有setListAdapter和所有ListView创建逻辑移至onActivityCreated()。在onCreateView()方法中,只需膨胀并返回rootView。