【问题标题】:"Class is Already defined" error“类已定义”错误
【发布时间】:2016-10-14 08:23:54
【问题描述】:

我有 listview 应用程序探索城市,每行指向不同的城市,在每个城市活动中包括按钮,当单击打开新活动时,无限画廊包含该城市的照片,我将无限画廊添加到第一个城市并且工作正常,当我想将它添加到第二个城市,它给了我类中的红色标记错误如下:

1- InfiniteGalleryAdapter 类型已定义。

2-InfiniteGallery 类型已定义。

我尝试用相同的结果更改类名,我删除了 R.java 并 Eclipse 以相同的结果重建它。此外,我从项目属性中取消选中 java builder 并得到相同的红色标记错误。

任何帮助和建议将不胜感激

谢谢

我的代码:

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

【问题讨论】:

  • 你能把代码块分成不同的类吗?很难看出现在课程从哪里开始和结束。
  • 这有点多。你认为你可以缩小出现错误的具体部分吗?
  • @Makoto 我发布了整个类,该项目只包含一个无限画廊类,是合并原始 4 类的结果,我在上面的代码中也写了我得到错误的地方,谢谢
  • 你试过清理你的项目吗?浏览您的 src/bin 文件夹以查看合并之前是否有一些剩余文件。
  • @Torious 是的,我清理它并浏览它,那里什么都没有,仍然是同样的错误

标签: java android eclipse


【解决方案1】:

你得到了那些红色标记,因为这些类已经在你之前的一个类中定义过。 Java 不允许有重复的名称。此外,您似乎正在尝试为每个冗余的 Actvitiy 定义相同的类。

只需从您的 SecondActivity java 文件中完全删除这两个类,因为它们已在先前的活动中定义。我建议您制作一个单独的包,其中定义了 Adapter 和 InfiniteGallery 并保持重复使用。

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

【讨论】:

    【解决方案2】:

    您甚至可能在同一文件夹中的其他文件上定义了具有相同名称的类。这可能会导致此错误。

    【讨论】:

      【解决方案3】:

      使用 Ctrl+H -> Java 搜索 -> 搜索类型 它会告诉你重复的类在哪里。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多