问题描述:

I am creating a Custom ArrayAdapter as per this, however I am getting error on this line:

Drawer drawer_data[] = new Drawer[] {

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

}

}

Activity

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

I looked at this but I don't know how to modify my code.

New Error: NullPointerEception stack trace:

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)

EDIT: Line 42 is - holder.mIcon.setImageResource(drawer.mIcon);

EDIT 2:

  1. Where do I add these setter and getter methods? Which .java file?

  2. Where do I call the drawer.getIcon().

    public int getIcon() {

    return this.mIcon;

    }

    public String getTitle() {

    return this.mText;

    }

网友答案:

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]);
}
网友答案:

Try this way

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);
相关阅读:
Top