0

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

Drawer drawer_data[] = new Drawer[] {

Variable must provide either dimension expressions or an array

抽屉.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;
    }
}

抽屉适配器.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);

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

新错误: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;
    }
    
4

2 回答 2

1

Try

Drawer drawer_data[] = new Drawer[mDrawerItems.size()];
for(int i = 0; i < mDrawerItems.size(); i++){
drawer_data[i] = new Drawer(mDrawerIcons[i],mDrawerItems[i]);
}
于 2013-08-31T13:14:30.407 回答
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);
于 2013-08-31T13:00:24.717 回答