ImageAdapter上的java.lang.NullPointerException。<init>(ImageAdapter.java:41)</init>

时间:2014-07-08 06:24:38

标签: java android android-fragments baseadapter

我在初始化ImageAdapter对象时遇到nullpointer异常。我已经定义了我的IMageAdapter参数化构造函数并正确调用它(希望如此)。我不知道我哪里错了?

我创建了单独的ImageApdater java类,并没有将其子类化,并且我创建此imageadapter的gridview是在Fragment上,而此Fragment是Tab。

这是我的ImageAdapter类

public class ImageAdapter extends BaseAdapter {

Context mcontext;
int[] IMAGES;
int[] IMAGE_ID,  IMAGE_PRICE;
String[] IMAGE_NAME,  MODEL_TYPE;

public ImageAdapter(Context context, int[] images, int[] image_id, int [] image_price,   
String[] image_name, String[] model_type) {

    mcontext=context;
    this.IMAGES = images;
    this.IMAGE_ID=image_id;
    this.IMAGE_PRICE=image_price;
    this.IMAGE_NAME=image_name;
    this.MODEL_TYPE=model_type;
}//ImageAdapter Constructor

我将在Fragment类中调用它,如下所示:

public class CommunityGalleryFragment extends Fragment {


    byte[] image = null;
    Bitmap bitmapImage = null;
    Context context;

  // images to be inflated in gallery view

// Images to be stored in database table
int[] IMAGES = { R.drawable.a, R.drawable.g, R.drawable.c,
        R.drawable.h, R.drawable.j, R.drawable.d, R.drawable.f,
        R.drawable.e, R.drawable.i, R.drawable.b };

// Spinner data
String[] VIEWALL_ITEMS = { "View All", "Glaze ", "Ceramic", "Plaster" };
String[] SORT_ITEMS = { "Sort By", "name", "Date" };

// Cursor to get records from table
Cursor initial_cursor;
RMLocalGalleryAdapter rmLocalGalleryAdapter;

// Data to be stored in database table

// Image ID's
int[] IMAGE_ID = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Image Price
int[] IMAGE_PRICE = { 32, 76, 25, 30, 50, 22, 33, 55, 65, 43 };
String[] IMAGE_NAME = {"a", "g","c", "h", "j", "d", "f", "e", "i","b"};

// Model Types
String[] MODEL_TYPE = { "Glaze", "Ceramic", "Ceramic", "Ceramic", "Glaze",
        "Glaze", "Plaster", "Plaster", "Plaster", "Plaster" };

String date;
// Declaring views
public Spinner viewAll_spinner, sort_spinner;
GridView gridView;

//boolean fromHomeActivity, fromLocalGallery;


public CommunityGalleryFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // inflating communitygallery_fragment layout from layout folder
    this.context = container.getContext();

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

    viewAll_spinner = (Spinner) view    
.findViewById(R.id.communityGallery_spinner_viewAll);
    sort_spinner = (Spinner) view.findViewById(R.id.communityGallery_spinner_sort);

    rmLocalGalleryAdapter = new RMLocalGalleryAdapter(getActivity());
    rmLocalGalleryAdapter.openDB();

    initial_cursor = rmLocalGalleryAdapter.getAllImagesByDate();

    // Setup UI
    gridView = (GridView) view.findViewById(R.id.communityGallery_gridView);


    // inserting data into sqlite database only if there is no data inserted previously
    if (initial_cursor.getCount() == 0) {
        // inserting data in multiple records---- it will be inserted as
        // many as images are available in IMAGES
        for (int i = 0; i < IMAGES.length; i++) {
            byte[] image = convertToByteArray(IMAGES[i]);

            // /calling insertValues method of Adapter class and passing all
            // paramenters
            rmLocalGalleryAdapter.insertValues(image, IMAGE_ID[i],  IMAGE_PRICE[i], 
IMAGE_NAME[i], MODEL_TYPE[i]);
        }// for
    }// if
        // closing cursor to avoid memory leaks
    initial_cursor.close();
    return view;
}// onCreateView

private byte[] convertToByteArray(int image) {
    Resources resources = getResources();
    Drawable drawable = resources.getDrawable(image);
    Bitmap bitmap =  ((BitmapDrawable)drawable).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress( Bitmap.CompressFormat.PNG, 100, stream);
    byte[] bitmapData = stream.toByteArray();

    return bitmapData;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    // creating adapter for " View All" Spinner
    ArrayAdapter<String> viewAll_Adapter = new ArrayAdapter<String>(
            getActivity(), android.R.layout.simple_spinner_dropdown_item, 
VIEWALL_ITEMS);

    // Creating adapter for " Sort By "Spinner
    ArrayAdapter<String> sortBy_Adapter = new ArrayAdapter<String>(
            getActivity(), android.R.layout.simple_spinner_dropdown_item, SORT_ITEMS);

    viewAll_spinner.setAdapter(viewAll_Adapter);
    sort_spinner.setAdapter(sortBy_Adapter);



    // setting ImageAdapter to the gridview


    //View spinner listener to handle selection in spinner
    viewAll_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,    int position, 
 long id) {
            // TODO Auto-generated method stub

            gridView.setAdapter(new ImageAdapter(context, IMAGES, IMAGE_ID, 
 IMAGE_PRICE, IMAGE_NAME, MODEL_TYPE ));

        }//view spinner 

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }//view spinner
    });
    sort_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,    int position, 
long id) {
            // TODO Auto-generated method stub

            gridView.setAdapter(new ImageAdapter(context, IMAGES, IMAGE_ID, 
IMAGE_PRICE, IMAGE_NAME, MODEL_TYPE ));
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    });

}// onActivityCreated
}

1 个答案:

答案 0 :(得分:0)

  

/ *这是第41行* / int view_spinner_id = communityGalleryFragment.viewAll_spinner.getSelectedItemPosition();

所以communityGalleryFragment.viewAll_spinner为空。你过早地试图使用它。

如果它是一个片段中的spiner,顾名思义,你将不得不等待片段本身在片段生命周期内初始化自己。实际上它最早是onCreateView()

另外,引用这样的另一个类内部并不是一个好的设计实践。