Wafer
Wafer

Reputation: 188

Why am I getting a Constructor Undefined error?

Why is this line:

g.setAdapter(new ImageAdapter(this));  

giving me the error "Constructor Undefined"?

I used this guide to make tabs http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/ and now I'm trying to implement a grid view on one of my tabs but there is a problem with the ImageAdapter. If I mouse over the error and change the code based on what it says than it just starts changing the "Context" stuff and making more errors.

Here is the code:

package com.example.tabexample;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class FragmentTab2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment2, container, false);


    GridView g = (GridView) rootView.findViewById(R.id.grid_view);
    g.setAdapter(new ImageAdapter(this));


    return rootView;

}
public class ImageAdapter extends BaseAdapter{
    private Context context;
    public Integer[] Skins = {R.drawable.s1, R.drawable.s2, R.drawable.s3,
            R.drawable.s4, R.drawable.s5, R.drawable.s6,
            R.drawable.s7, R.drawable.s8, R.drawable.s9,
            R.drawable.s10, R.drawable.s11, R.drawable.s12};

    public ImageAdapter(Context c) {
        // TODO Auto-generated constructor stub
        context = c;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView pic = new ImageView(context);
        pic.setLayoutParams(new GridView.LayoutParams(200,200));
        pic.setScaleType(ImageView.ScaleType.FIT_CENTER);
        pic.setPadding(8, 8, 8, 8);
        pic.setImageResource(Skins[position]);
        return pic;
    }

}

}

Upvotes: 1

Views: 466

Answers (1)

Szymon
Szymon

Reputation: 43023

Replace that line with

g.setAdapter(new ImageAdapter(getActivity()));

Fragment doesn't inherit from Context.

Upvotes: 4

Related Questions