Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android CustomAdapter-变量必须提供维度表达式或数组_Android_Listview_Android Arrayadapter - Fatal编程技术网

Android CustomAdapter-变量必须提供维度表达式或数组

Android CustomAdapter-变量必须提供维度表达式或数组,android,listview,android-arrayadapter,Android,Listview,Android Arrayadapter,我正在根据创建自定义ArrayAdapter,但在这一行中出现错误: Drawer\u data[]=新抽屉[]{ 该变量必须提供维度表达式或数组 Drawer.java public class Drawer { public int mIcon; public String mText; public Drawer() { super(); } public Drawer(int mIcon, String mTitle) {

我正在根据创建自定义ArrayAdapter,但在这一行中出现错误:

Drawer\u data[]=新抽屉[]{

变量必须提供维度表达式或数组

Drawer.java

public class Drawer {
    public int mIcon;
    public String mText;

    public Drawer() {
        super();
    }

    public Drawer(int mIcon, String mTitle) {
        super();
        this.mIcon = mIcon;
        this.mText = mTitle;
    }
}
public class DrawerAdapter extends ArrayAdapter<Drawer> {

    Context context;
    int layoutResourceId;
    Drawer data[] = null;

    public Drawer_CustomAdapter(Context context, int layoutResourceId, Drawer[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DrawerHolder holder = null;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) ((Activity)context).getLayoutInflater();

            convertView = inflater.inflate(layoutResourceId, parent, false);
            holder = new DrawerHolder();
            holder.mIcon = (ImageView) convertView.findViewById(R.id.drawer_icon);
            holder.mText = (TextView) convertView.findViewById(R.id.drawer_text);
            convertView.setTag(holder);
        } else {
            holder = (DrawerHolder) convertView.getTag();
        }

        Drawer drawer = data[position];
        holder.mIcon.setImageResource(drawer.mIcon);
        holder.mText.setText(drawer.mText);

        return convertView;
    }

    static class DrawerHolder {
        ImageView mIcon;
        TextView mText;
    }
}
draweradopter.java

public class Drawer {
    public int mIcon;
    public String mText;

    public Drawer() {
        super();
    }

    public Drawer(int mIcon, String mTitle) {
        super();
        this.mIcon = mIcon;
        this.mText = mTitle;
    }
}
public class DrawerAdapter extends ArrayAdapter<Drawer> {

    Context context;
    int layoutResourceId;
    Drawer data[] = null;

    public Drawer_CustomAdapter(Context context, int layoutResourceId, Drawer[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DrawerHolder holder = null;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) ((Activity)context).getLayoutInflater();

            convertView = inflater.inflate(layoutResourceId, parent, false);
            holder = new DrawerHolder();
            holder.mIcon = (ImageView) convertView.findViewById(R.id.drawer_icon);
            holder.mText = (TextView) convertView.findViewById(R.id.drawer_text);
            convertView.setTag(holder);
        } else {
            holder = (DrawerHolder) convertView.getTag();
        }

        Drawer drawer = data[position];
        holder.mIcon.setImageResource(drawer.mIcon);
        holder.mText.setText(drawer.mText);

        return convertView;
    }

    static class DrawerHolder {
        ImageView mIcon;
        TextView mText;
    }
}
编辑:第42行是-
holder.mIcon.setImageResource(drawer.mIcon);

编辑2:

  • 在哪里添加这些setter和getter方法?哪个.java文件

  • 我在哪里调用
    抽屉.getIcon()


  • 这样试试看

        Drawer drawer_data[] = new Drawer[] { // <<<< ERROR
                for(int i=0; i<mDrawerItems; i++) {
                    new Drawer(mDrawerIcons[i],mDrawerItems[i]);
                }
        };
    
        DrawerAdaper adapter = new DrawerAdapter(this,R.layout.drawer_list_item,drawer_data);
    
        mDrawerList.setAdapter(adapter);
    
    Drawer drawer_data[] = new Drawer[mDrawerItems.length]
    
                 for(int i=0; i<mDrawerItems.length; i++) {
                    drawer_data[]=new Drawer(mDrawerIcons[i],mDrawerItems[i]);
                }
    
    
        DrawerAdaper adapter = new DrawerAdapter(this,R.layout.drawer_list_item,drawer_data);
    
        mDrawerList.setAdapter(adapter);
    
    Drawer\u data[]=新抽屉[mDrawerItems.length]
    对于(inti=0;iTry

    Drawer-Drawer_-data[]=新抽屉[mDrawerItems.size()];
    对于(int i=0;i
    试试这个
    Drawer-Drawer\u data[]=new-Drawer[mDrawerItems.size()]
    这解决了这个错误。但是当我运行应用程序时,它在运行时崩溃了:错误:Drawer\u CustomAdapter.java行42:holder.mIcon.setImageResource(Drawer.mIcon)
    有什么想法吗?发表在上面的主要帖子中。在抽屉类中使用setter和getter。然后调用
    drawer.getIcon()
    我怎么做?对不起,我还是初学者…:(已尝试。错误已解决,但应用程序在运行时崩溃。错误:NullPOinter:请参阅上面问题的注释。已尝试。相同的错误。我认为需要使用
    setter
    getter
    方法。尝试登录以确定holder.mIcon是否为null或drawer.mIcon是否为null。您告诉的上述答案解决了此问题。以下代码r Drawer类位于上面的主帖子中,以粗体显示为“Drawer”。您设置了mDrawerIcons的值吗?也就是说,mDrawerIcons[i]不是空的,对吗?
    Drawer drawer_data[] = new Drawer[mDrawerItems.size()];
    for(int i = 0; i < mDrawerItems.size(); i++){
    drawer_data[i] = new Drawer(mDrawerIcons[i],mDrawerItems[i]);
    }