gridview在真正的Android设备崩溃,但在Android模拟器中工作正常

时间:2017-03-15 09:03:14

标签: android

我有一个用于在gridview中显示图像的片段,它在点击naviagation抽屉项目时打开,它与android emualtor工作正常但它在真正的Android设备上崩溃

这是我的frament_info_photos

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.abcd.InfoPhotos">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:textSize="25sp"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:textStyle="normal"
        android:text="photos" />
    <View
        android:layout_width="75dp"
        android:layout_height="5dp"
        android:layout_marginLeft="20dp"
        android:background="@color/Shade_browm"/>
    <GridView
        android:layout_marginTop="12dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:id="@+id/photos_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnWidth="100dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"></GridView>

</LinearLayout>

InfoPhotos.java

public class InfoPhotos extends Fragment {
    GridView gridView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_info_photos, container, false);
        gridView = (GridView) view.findViewById(R.id.photos_fragment);
        gridView.setAdapter(new ImageAdapter(getActivity()));
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {


            }
        });
        return view;
    }

}

和我的ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
    private int images[] ={R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5,
            R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9};
    Context context;

    public ImageAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView imageView;
        if(arg1 == null)
        {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(90, 90));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(0, 0, 0, 0);
        }
        else {
            imageView = (ImageView) arg1;
        }
        imageView.setImageResource(images[arg0]);
        return imageView;
    }
}

我不知道代码有什么问题

0 个答案:

没有答案