为什么我得到一个构造函数未定义的错误? [英] Why am I getting a Constructor Undefined error?

查看:741
本文介绍了为什么我得到一个构造函数未定义的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是为什么行:

  g.setAdapter(新ImageAdapter(本));

给我的错误构造未​​定义?

我用这个指南,使标签 HTTP:// WWW。 androidbegin.com/tutorial/implementing-fragment-tabs-in-android/ ,现在我想要实现我的标签之一网格视图,但没有与ImageAdapter一个问题。如果我将鼠标放在错误,改变基于它说什么比它刚刚起步改变背景的东西,使更多的错误,code。

下面是code:

 包com.example.tabexample;进口android.app.Fragment;
进口android.content.Context;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.GridView;
进口android.widget.ImageView;公共类FragmentTab2扩展片段{
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
    查看rootView = inflater.inflate(R.layout.fragment2,集装箱,FALSE);
    GridView控件G =(GridView控件)rootView.findViewById(R.id.grid_view);
    g.setAdapter(新ImageAdapter(本));
    返回rootView;}
公共类ImageAdapter延伸BaseAdapter {
    私人上下文的背景下;
    公共整数[] =皮肤{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};    公共ImageAdapter(上下文C){
        // TODO自动生成构造函数存根
        上下文= C;
    }    @覆盖
    公众诠释的getCount(){
        // TODO自动生成方法存根
        返回0;
    }    @覆盖
    公共对象的getItem(INT位置){
        // TODO自动生成方法存根
        返回null;
    }    @覆盖
    众长getItemId(INT位置){
        // TODO自动生成方法存根
        返回0;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        // TODO自动生成方法存根
        ImageView的PIC =新ImageView的(上下文);
        pic.setLayoutParams(新GridView.LayoutParams(200,200));
        pic.setScaleType(ImageView.ScaleType.FIT_CENTER);
        pic.setPadding(8,8,8,8);
        pic.setImageResource(皮肤[位置]);
        返回图片;
    }}}


解决方案

替换该行以

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

片段不会从上下文继承

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;
    }

}

}

解决方案

Replace that line with

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

Fragment doesn't inherit from Context.

这篇关于为什么我得到一个构造函数未定义的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆