"班级已经被定义"错误 [英] "Class is Already defined" error

查看:145
本文介绍了"班级已经被定义"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ListView应用探索城市每一行点不同势城市,在每个城市的活动用户点击打开新的活动是无限的库中包含了这个城市的图片时,包括按钮,我增添了无穷的画廊第一座城市,做工精细,当我希望将其添加到二线城市,它给了我红色标记错误的类如下:

1类型InfiniteGalleryAdapter已定义。

2 - 类型InfiniteGallery已定义

我试图改变类名相同的结果,我删除R.java和eclipse有相同的结果重建。此外,我未选中从项目属性时,Java Builder并获得相同的红色标记错误。

请任何帮助和建议将成为AP preciated

感谢

我的code:

 公共类SecondCity延伸活动{    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        布尔customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        //设置布局时使用
        的setContentView(R.layout.main);
        如果(customTitleSupported){
            。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
            TextView的电视=(的TextView)findViewById(R.id.tv);
            字体脸= Typeface.createFromAsset(getAssets(),BFantezy.ttf);
            tv.setTypeface(面);
            tv.setText(图片);
        }        InfiniteGallery galleryOne =(InfiniteGallery)findViewById(R.id.galleryOne);
        galleryOne.setAdapter(新InfiniteGalleryAdapter(本));
    }
}
类InfiniteGalleryAdapter扩展了BaseAdapter {
    ** //红色标记位置(InfiniteGalleryAdapter)**
    私人语境mContext;    公共InfiniteGalleryAdapter(上下文C中,int [] imageIds){
        this.mContext = C;
    }    公众诠释的getCount(){
        返回Integer.MAX_VALUE的;
    }    公共对象的getItem(INT位置){
        返回的位置;
    }    众长getItemId(INT位置){
        返回的位置;
    }    私人LayoutInflater吹气= NULL;
    公共InfiniteGalleryAdapter(背景信息){
        this.mContext =一;
        吹气=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
    }    公共类ViewHolder {
        公众的TextView文本;
        公众形象ImageView的;
    }    私人诠释[] =图像{
        R.drawable.pic_1,R.drawable.pic_2,
        R.drawable.pic_3,R.drawable.pic_4,
        R.drawable.pic_5
    };    私有String [] NAME = {
        这是第一图片(1),
        这是第二画面(2),
        这是第三张照片(3),
        这是第四个画面(4),
        这是第五个画面(5)
    };    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        ImageView的I = getImageView();        INT itemPos =(位置%images.length);        尝试{
            i.setImageResource(图像[itemPos]); ((BitmapDrawable)i.getDrawable())。
            setAntiAlias​​(真);
        }        赶上(OutOfMemoryError异常五){
            Log.e(InfiniteGalleryAdapter,内存不足创建的ImageView使用空视图。E);
        }        查看VI = convertView;
        ViewHolder持有人;
        如果(convertView == NULL){
            VI = inflater.inflate(R.layout.gallery_items,NULL);
            持有人=新ViewHolder(); holder.text =(TextView中)vi.findViewById(R.id.textView1);
            holder.image =(ImageView的)vi.findViewById(R.id.image);
            vi.setTag(保持器);
        }其他{
            支架=(ViewHolder)vi.getTag();
        }
        holder.text.setText(名称[itemPos]);        最终诠释stub_id =图像[itemPos]
        holder.image.setImageResource(stub_id);        返回VI;
    }    私人ImageView的getImageView(){        ImageView的I =新ImageView的(mContext);        返回我;
    }
}类InfiniteGallery扩展库{
    ** //红色标记位置(InfiniteGallery)**    公共InfiniteGallery(上下文的背景下){
        超级(上下文);
        在里面();
    }    公共InfiniteGallery(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        在里面();
    }    公共InfiniteGallery(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        在里面();
    }    私人无效的init(){
        //这些只是使它看起来pretty
        setSpacing(25);
        setHorizo​​ntalFadingEdgeEnabled(假);
    }    公共无效setResourceImages(INT []名){
        setAdapter(新InfiniteGalleryAdapter(的getContext(),名));
        setSelection((的getCount()/ 2));
    }
}


解决方案

您也越来越那些红色标记,因为这些类是在你的previous类的一个已定义。 Java将不会让有重名。此外,它似乎您试图定义每个Actvitiy这是多余的相同的类。


因为它们是在previous活动已经定义只是从你的SecondActivity的Java文件彻底删除两班。我建议你​​做一个单独的包,其中适配器和InfiniteGallery的定义和保持重用。

 公共类SecondCity延伸活动{    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        布尔customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        //设置布局时使用
        的setContentView(R.layout.main);
        如果(customTitleSupported){
            。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
            TextView的电视=(的TextView)findViewById(R.id.tv);
            字体脸= Typeface.createFromAsset(getAssets(),BFantezy.ttf);
            tv.setTypeface(面);
            tv.setText(图片);
        }        InfiniteGallery galleryOne =(InfiniteGallery)findViewById(R.id.galleryOne);
        galleryOne.setAdapter(新InfiniteGalleryAdapter(本));
    }
}

I have listview app exploring cities each row point to diffrent city , in each city activity include button when clicked open new activity which is infinite gallery contains pics of that city , i add infinite gallery to first city and work fine , when i want to add it to the second city , it gave me red mark error in the class as follow :

1- The type InfiniteGalleryAdapter is already defined.

2-The type InfiniteGallery is already defined.

I tried to change class name with the same result, I delete R.java and eclipse rebuild it with same result. Also I unchecked the java builder from project properties and get same red mark error.

Please any help and advice will be appreciated

thanks

My Code:

public class SecondCity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
        // Set the layout to use
        setContentView(R.layout.main);
        if (customTitleSupported) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); 
            TextView tv = (TextView) findViewById(R.id.tv); 
            Typeface face=Typeface.createFromAsset(getAssets(),"BFantezy.ttf");     
            tv.setTypeface(face);
            tv.setText("MY PICTURES"); 
        }

        InfiniteGallery galleryOne = (InfiniteGallery) findViewById(R.id.galleryOne);
        galleryOne.setAdapter(new InfiniteGalleryAdapter(this));     
    }
}


class InfiniteGalleryAdapter extends BaseAdapter { 
    **//red mark here (InfiniteGalleryAdapter)** 
    private Context mContext;

    public InfiniteGalleryAdapter(Context c, int[] imageIds) { 
        this.mContext = c;
    } 

    public int getCount() { 
        return Integer.MAX_VALUE;
    } 

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

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

    private LayoutInflater inflater=null;
    public InfiniteGalleryAdapter(Context a) { 
        this.mContext = a; 
        inflater = (LayoutInflater)mContext.getSystemService ( Context.LAYOUT_INFLATER_SERVICE)
    } 

    public class ViewHolder{ 
        public TextView text; 
        public ImageView image;
    } 

    private int[] images = {
        R.drawable.pic_1, R.drawable.pic_2, 
        R.drawable.pic_3, R.drawable.pic_4, 
        R.drawable.pic_5
    }; 

    private String[] name = {
        "This is first picture (1) " ,
        "This is second picture (2)",
        "This is third picture (3)", 
        "This is fourth picture (4)",
        " This is fifth picture (5)"
    }; 

    public View getView(int position, View convertView, ViewGroup parent) { 
        ImageView i = getImageView(); 

        int itemPos = (position % images.length); 

        try {
            i.setImageResource(images[itemPos]); ((BitmapDrawable) i.getDrawable()).
            setAntiAlias  (true);
        } 

        catch (OutOfMemoryError e) {
            Log.e("InfiniteGalleryAdapter", "Out of memory creating imageview. Using empty view.", e);
        } 

        View vi=convertView; 
        ViewHolder holder; 
        if(convertView==null){ 
            vi = inflater.inflate(R.layout.gallery_items, null); 
            holder=new ViewHolder(); holder.text=(TextView)vi.findViewById(R.id.textView1); 
            holder.image=(ImageView)vi.findViewById(R.id.image); 
            vi.setTag(holder);
        } else {
            holder=(ViewHolder)vi.getTag();
        } 
        holder.text.setText(name[itemPos]); 

        final int stub_id=images[itemPos]; 
        holder.image.setImageResource(stub_id); 

        return vi;
    } 

    private ImageView getImageView() { 

        ImageView i = new ImageView(mContext); 

        return i;
    } 
}

class InfiniteGallery extends Gallery {
    **//red mark here (InfiniteGallery)** 

    public InfiniteGallery(Context context) {
        super(context);
        init();
    } 

    public InfiniteGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public InfiniteGallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init(){
        // These are just to make it look pretty
        setSpacing(25);
        setHorizontalFadingEdgeEnabled(false);
    } 

    public void setResourceImages(int[] name){
        setAdapter(new InfiniteGalleryAdapter(getContext(), name));
        setSelection((getCount() / 2));
    }
}

解决方案

You are getting those red marks as these class are already defined in one of your previous class. Java won't let have duplicate names. Also it seems you are trying to define same classes for every Actvitiy which is redundant.

Just remove the two classes completely from your SecondActivity java file as they are already defined in a previous activity. I would suggest you to make a separate package where the Adapter and InfiniteGallery are defined and kept to be reused .

public class SecondCity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
        // Set the layout to use
        setContentView(R.layout.main);
        if (customTitleSupported) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); 
            TextView tv = (TextView) findViewById(R.id.tv); 
            Typeface face=Typeface.createFromAsset(getAssets(),"BFantezy.ttf");     
            tv.setTypeface(face);
            tv.setText("MY PICTURES"); 
        }

        InfiniteGallery galleryOne = (InfiniteGallery) findViewById(R.id.galleryOne);
        galleryOne.setAdapter(new InfiniteGalleryAdapter(this));     
    }
}

这篇关于"班级已经被定义"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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