为什么我无法将自定义适配器的网格视图导入到片段中?

时间:2014-03-05 10:10:37

标签: android gridview android-fragments custom-adapter

我想要做的是查看带有自定义适配器的网格视图到片段,因为我想使用带有滑动过滤器的视图寻呼机..这里是我的代码:

 public class KitchenBlinds extends Fragment {
        public Integer[] mThumbIds = {
                R.drawable.kit_1, R.drawable.kit_2,
                R.drawable.kit_3, R.drawable.kit_4,
                R.drawable.kit_5, R.drawable.kit_6,
                R.drawable.kit_7, R.drawable.kit_8,
                R.drawable.kit_9, R.drawable.kit_10,
                R.drawable.kit_11, R.drawable.kit_12,
                R.drawable.kit_13, R.drawable.kit_14,
                R.drawable.kit_15
        };
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.activity_kitchen_blinds, container, false);

            return view;




        GridView gridview = (GridView)view.findViewById(R.id.grid_view);
          try{
            // Instance of ImageAdapter Class
                gridview.setAdapter(new ImageAdapter(getActivity(), mThumbIds)); 
          } catch (OutOfMemoryError E) { 
            E.printStackTrace();
        }


            /**
             * On Click event for Single Gridview Item
             * */
            gridview.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

                    // Sending image id to FullScreenActivity
                    Intent i = new Intent(KitchenBlinds.this, FullImageActivity2.class);
                    // passing array index
                     i.putExtra("id", mThumbIds[position]);
                        Log.d("ID", "" + mThumbIds[position]);
                         startActivity(i);
                }
            });
        }




        }

但我一直看到这行错误:

 GridView gridview = (GridView)view.findViewById(R.id.grid_view);

也在这一个:

  Intent i = new Intent(KitchenBlinds.this, FullImageActivity2.class);

你可以帮我把网格视图用到片段吗???

这是我的kitchen_blinds_layout代码:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:columnWidth="90dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:gravity="center"
    android:stretchMode="columnWidth" >  

</GridView>

4 个答案:

答案 0 :(得分:2)

主要错误是返回视图。这样做

public class KitchenBlinds extends Fragment {
            public Integer[] mThumbIds = {
                    R.drawable.kit_1, R.drawable.kit_2,
                    R.drawable.kit_3, R.drawable.kit_4,
                    R.drawable.kit_5, R.drawable.kit_6,
                    R.drawable.kit_7, R.drawable.kit_8,
                    R.drawable.kit_9, R.drawable.kit_10,
                    R.drawable.kit_11, R.drawable.kit_12,
                    R.drawable.kit_13, R.drawable.kit_14,
                    R.drawable.kit_15
            };
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {

                View view = inflater.inflate(R.layout.activity_kitchen_blinds, container, false);

                GridView gridview = (GridView)view.findViewById(R.id.grid_view);
              try{
                // Instance of ImageAdapter Class
                    gridview.setAdapter(new ImageAdapter(getActivity(), mThumbIds)); 
              } catch (OutOfMemoryError E) { 
                E.printStackTrace();
            }


                /**
                 * On Click event for Single Gridview Item
                 * */
                gridview.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View v,
                            int position, long id) {

                        // Sending image id to FullScreenActivity
                        Intent i = new Intent(KitchenBlinds.this, FullImageActivity2.class);
                        // passing array index
                         i.putExtra("id", mThumbIds[position]);
                            Log.d("ID", "" + mThumbIds[position]);
                             startActivity(i);
                    }
                });
            }

      return view;


            }

答案 1 :(得分:1)

的第一个参数
 Intent i = new Intent(KitchenBlinds.this, FullImageActivity2.class);

Context,但KitchenBlinds是片段。你应该用getActivity()

改变它
 Intent i = new Intent(getActivity(), FullImageActivity2.class);

另外,你回来的时间太早了。 return语句应该是onCreateView内的最后一行代码,否则会出现编译时错误。

return view

的末尾移动onCreateView

答案 2 :(得分:1)

您得到的第一个错误是因为您没有在项目中导入网格视图,因此首先按Ctrl + Shift + o(在eclipse中)导入它。

您在下面一行中出现的第二个错误

Intent i = new Intent(KitchenBlinds.this, FullImageActivity2.class);

错了。由于你使用的是片段,你必须像这样使用它。

gridview.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

                    // Sending image id to FullScreenActivity
                        Intent myIntent = new Intent((MainActivity) getActivity(),FullImageActivity2.class);
                    // passing array index
                     myIntent .putExtra("id", mThumbIds[position]);
                        Log.d("ID", "" + mThumbIds[position]);
                         getActivity().startActivity(myIntent);
                }
            });

而且你忘了回复你的观点。

所以基本上你的完整代码会像这样。

public class KitchenBlinds extends Fragment {
        public Integer[] mThumbIds = {
                R.drawable.kit_1, R.drawable.kit_2,
                R.drawable.kit_3, R.drawable.kit_4,
                R.drawable.kit_5, R.drawable.kit_6,
                R.drawable.kit_7, R.drawable.kit_8,
                R.drawable.kit_9, R.drawable.kit_10,
                R.drawable.kit_11, R.drawable.kit_12,
                R.drawable.kit_13, R.drawable.kit_14,
                R.drawable.kit_15
        };
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.activity_kitchen_blinds, container, false);

        GridView gridview = (GridView)view.findViewById(R.id.grid_view);
          try{
            // Instance of ImageAdapter Class
                gridview.setAdapter(new ImageAdapter(getActivity(), mThumbIds)); 
          } catch (OutOfMemoryError E) { 
            E.printStackTrace();
        }


            /**
             * On Click event for Single Gridview Item
             * */
             gridview.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

                    // Sending image id to FullScreenActivity
                        Intent myIntent = new Intent((MainActivity) getActivity(),FullImageActivity2.class);
                    // passing array index
                     myIntent .putExtra("id", mThumbIds[position]);
                        Log.d("ID", "" + mThumbIds[position]);
                         getActivity().startActivity(myIntent);
                }
            });
               return view;
        }
        }

答案 3 :(得分:0)

在onCreateView函数

的末尾写return view;

也改变了

Intent i = new Intent(KitchenBlinds.this, FullImageActivity2.class);

Intent i = new Intent(getActivity(), FullImageActivity2.class);

onCreateView:

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

       View view = inflater.inflate(R.layout.activity_kitchen_blinds, container, false);

        GridView gridview = (GridView)view.findViewById(R.id.grid_view);
          try{
            // Instance of ImageAdapter Class
                gridview.setAdapter(new ImageAdapter(getActivity(), mThumbIds)); 
          } catch (OutOfMemoryError E) { 
            E.printStackTrace();
        }

            /**
             * On Click event for Single Gridview Item
             * */
            gridview.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

                    // Sending image id to FullScreenActivity
                    Intent i = new Intent(KitchenBlinds.this, FullImageActivity2.class);
                    // passing array index
                     i.putExtra("id", mThumbIds[position]);
                        Log.d("ID", "" + mThumbIds[position]);
                         startActivity(i);
                }
            });

            return view;//return here

        }