简体   繁体   English

为什么我收到构造函数未定义错误?

[英]Why am I getting a Constructor Undefined error?

Why is this line: 为什么这一行:

g.setAdapter(new ImageAdapter(this));  

giving me the error "Constructor Undefined"? 给我错误“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. 我使用本指南制作标签http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/现在我正在尝试在我的某个标签上实现网格视图,但是存在问题使用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;
    }

}

}

Replace that line with 用。替换该行

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

Fragment doesn't inherit from Context . Fragment不会从Context继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM