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

时间:2013-08-31 12:51:50

标签: android listview android-arrayadapter

我正按照this创建自定义ArrayAdapter,但是我在这一行上收到错误:

Drawer drawer_data[] = new Drawer[] {

Variable must provide either dimension expressions or an array

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

DrawerAdapter.java

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

活动

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

我查看了this,但我不知道如何修改我的代码。

新错误:NullPointerEception堆栈跟踪:

08-31 14:05:39.604: E/AndroidRuntime(14724): FATAL EXCEPTION: main
08-31 14:05:39.604: E/AndroidRuntime(14724): java.lang.NullPointerException
08-31 14:05:39.604: E/AndroidRuntime(14724):    at com.TVGenius.DrawerAdapter.getView(DrawerAdapter.java:42)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.widget.AbsListView.obtainView(AbsListView.java:2461)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.widget.ListView.makeAndAddView(ListView.java:1775)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.widget.ListView.fillDown(ListView.java:678)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.widget.ListView.fillFromTop(ListView.java:739)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.widget.ListView.layoutChildren(ListView.java:1628)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.widget.AbsListView.onLayout(AbsListView.java:2296)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.View.layout(View.java:14055)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.ViewGroup.layout(ViewGroup.java:4604)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:672)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.View.layout(View.java:14055)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.ViewGroup.layout(ViewGroup.java:4604)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.View.layout(View.java:14055)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.ViewGroup.layout(ViewGroup.java:4604)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.View.layout(View.java:14055)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.ViewGroup.layout(ViewGroup.java:4604)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.View.layout(View.java:14055)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.ViewGroup.layout(ViewGroup.java:4604)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1992)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1813)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1112)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4472)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.Choreographer.doFrame(Choreographer.java:525)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.os.Handler.handleCallback(Handler.java:615)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.os.Looper.loop(Looper.java:137)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at android.app.ActivityThread.main(ActivityThread.java:4898)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at java.lang.reflect.Method.invokeNative(Native Method)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at java.lang.reflect.Method.invoke(Method.java:511)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
08-31 14:05:39.604: E/AndroidRuntime(14724):    at dalvik.system.NativeStart.main(Native Method)

编辑:第42行是 - holder.mIcon.setImageResource(drawer.mIcon);

编辑2:

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

  2. 我在哪里拨打drawer.getIcon()

    public int getIcon() {
        return this.mIcon;
    }
    
    public String getTitle() {
        return this.mText;
    }
    

2 个答案:

答案 0 :(得分:1)

试试这种方式

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

答案 1 :(得分:1)

尝试

Drawer drawer_data[] = new Drawer[mDrawerItems.size()];
for(int i = 0; i < mDrawerItems.size(); i++){
drawer_data[i] = new Drawer(mDrawerIcons[i],mDrawerItems[i]);
}