的Android CustomAdapter - 变量必须提供尺寸表达式或阵列
我创建一个定制ArrayAdapter按照this,但是我在这条线收到错误:的Android CustomAdapter - 变量必须提供尺寸表达式或阵列
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:
在哪里添加这些setter和getter方法?哪个.java文件?
-
我在哪里可以拨打
drawer.getIcon()
。public int getIcon() { return this.mIcon; } public String getTitle() { return this.mText; }
尝试
Drawer drawer_data[] = new Drawer[mDrawerItems.size()];
for(int i = 0; i < mDrawerItems.size(); i++){
drawer_data[i] = new Drawer(mDrawerIcons[i],mDrawerItems[i]);
}
尝试过,同样的错误,我认为需要一个'setter'和'getter'方法。 – KickAss
尝试记录以确定holder.mIcon是否为null或drawer.mIcon为null。 –
这是针对您告知固定问题的上述答案。 –
尝试这种方式
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);
试过了,错误解决了,但是应用程序在RunTime上崩溃。以上问题 – KickAss
试试这个'抽屉drawer_data [] =新的抽屉[mDrawerItems.size()]' – Raghunandan
这解决错误。但是当我运行该应用程序。它在RunTime上崩溃:Error:NullPointer in Drawer_CustomAdapter.java line 42:holder.mIcon.setImageResource(drawer.mIcon);'有什么想法? – KickAss
发布stakctrace – Raghunandan