我试图按照ImageView和GalleryView教程在片段类中添加galleryview,但是存在一个错误,即ImageAdapter无法应用于Tab1Fragment类,并且我不知道如何解决它。



片段类

package eu.lucazanini;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import eu.lucazanini.DialogHelper;


public class Tab2Fragment extends Fragment{


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tab2, container, false);

        GridView gridview = (GridView) gridview.findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this);

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                Toast.makeText(getActivity(), "" + position,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}


ImageAdapter类-仅在Fragment类内,此类内未显示任何错误

package eu.lucazanini;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) { // if it's not recycled, initialize some
            // attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 };
}

最佳答案

首先,应将返回行return inflater.inflate(R.layout.tab2, container, false);移至onCreateView的末尾,因为所有其他行均将无法访问。

另外,您应该通过以下方式更正onCreateView:

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

    GridView gridview = (GridView) view.findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(getActivity());

    .
    .
    .

    return view ;


您应该将上下文传递给它。您可以使用getActivity()。另外,您缺少右括号。因此,将错误的行替换为

new ImageAdapter(getActivity()) ;

08-18 09:45