【问题标题】:Getting an error when trying to create my own view class?尝试创建自己的视图类时出错?
【发布时间】:2013-04-29 11:52:26
【问题描述】:

大家好,我在 MainActivity 中尝试创建视图时遇到错误。下面我提供了一些来自我的自定义 GridView 和 Activity 的代码。出于某种原因,当我尝试将参数传递到 MainActivity 的 OnCreate 中的 GridView 时,我在 Eclipse 中收到以下错误:

构造函数 GridView(this, Cell[][]) 未定义

但在我的 GridView 类中,我显然提供了这样的构造函数。不确定是什么问题。我什至声明了额外的构造函数,因为有人建议过,但没有运气。我是在声明上下文正确还是在 XML 方面需要做些什么?有人可以告诉我我做错了什么吗?谢谢

自定义 GridView 布局:

public class GridView extends View {

    Context context;
    private Cell[][] grid; //Cell class - is an interface

    //constructors
    public GridView (Context context){
        super(context);
    }

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

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


    public GridView(Context context, Cell[][] grid) {
        super(context);
        this.context = context;
        this.grid = grid;
        //...more stuff
    }
    //...more stuff
}

主要活动:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    Cell[][] grid = new Cell[height][width];
    final GridView view = new GridView(this, grid);

    //...more stuff

}
//...more stuff 

}

【问题讨论】:

  • 向我们展示单元类声明
  • 可能导入错误?你能检查一下=吗?

标签: android android-layout gridview view android-custom-view


【解决方案1】:

我认为你的问题是你使用的类名已经在 Android (android.widget.GridView) 中实现,所以当你调用类的构造函数时,编译器可能会混淆它们。

要么将你的实现重命名为系统唯一的名称(例如MyGridView),要么专门调用你的类名,包括你的包名(Your.Namespace.GridView

【讨论】:

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