-1

我有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));
    }
}
4

3 回答 3

4

您正在获得那些红色标记,因为这些课程已经在您之前的课程中定义过。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));     
    }
}
于 2012-04-15T06:28:02.833 回答
0

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

于 2015-04-18T03:05:36.787 回答
0

使用 Ctrl+H -> Java Search -> Search for Type 它会告诉你重复的类在哪里。

于 2016-10-14T00:23:54.773 回答