在适配器

时间:2015-09-16 14:30:14

标签: android android-layout listview

我已经使用ImageView和TextView制作导航抽屉,它可以运行。但是,当我想创建带有两个ImageView和TextView的导航抽屉时,它将显示错误("尝试调用虚拟方法")使用我的第二个ImageView(我的代码中的变量'和#34; img")

我的代码是这样的:

public class MenuListAdapter extends BaseAdapter {

//...

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    TextView Title = null;
    ImageView Icon = null;
    ImageView img = null;

    if (v == null) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.drawer, parent, false);

        // Locate the TextViews in drawer_list_item.xml
        Title = (TextView) convertView.findViewById(R.id.title);

        // Locate the ImageView in drawer_list_item.xml
        Icon = (ImageView) convertView.findViewById(R.id.icon);

        // Locate the ImageView in drawer_list_item.xml
        img = (ImageView) convertView.findViewById(R.id.img);

    }

    Title.setText(mTitle[position]);

    Icon.setImageResource(mIcon[position]);
    img.setImageResource(mimg[position]);

    return v;
}

这是我的logcat:

  

09-16 22:25:38.458:E / AndroidRuntime(14966):致命异常:主

     

09-16 22:25:38.458:E / AndroidRuntime(14966):java.lang.NullPointerException:尝试调用虚拟方法' android.view.View android.view.View.findViewById(int)& #39;在null对象引用上   09-16 22:25:38.458:E / AndroidRuntime(14966):at com.test.adapter.MenuListAdapter.getView(MenuListAdapter.java:84)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.widget.AbsListView.obtainView(AbsListView.java:2344)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.widget.ListView.makeAndAddView(ListView.java:1864)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.widget.ListView.fillDown(ListView.java:698)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.widget.ListView.fillFromTop(ListView.java:759)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.widget.ListView.layoutChildren(ListView.java:1673)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.widget.AbsListView.onLayout(AbsListView.java:2148)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.View.layout(View.java:15686)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.ViewGroup.layout(ViewGroup.java:4967)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:914)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.View.layout(View.java:15686)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.ViewGroup.layout(ViewGroup.java:4967)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.widget.FrameLayout.onLayout(FrameLayout.java:508)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.View.layout(View.java:15686)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.ViewGroup.layout(ViewGroup.java:4967)   09-16 22:25:38.458:E / AndroidRuntime(14966):at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.View.layout(View.java:15686)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.ViewGroup.layout(ViewGroup.java:4967)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.widget.FrameLayout.onLayout(FrameLayout.java:508)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.View.layout(View.java:15686)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.ViewGroup.layout(ViewGroup.java:4967)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2076)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1833)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1058)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5796)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.Choreographer.doCallbacks(Choreographer.java:580)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.Choreographer.doFrame(Choreographer.java:550)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.os.Handler.handleCallback(Handler.java:739)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.os.Handler.dispatchMessage(Handler.java:95)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.os.Looper.loop(Looper.java:211)   09-16 22:25:38.458:E / AndroidRuntime(14966):在android.app.ActivityThread.main(ActivityThread.java:5333)   09-16 22:25:38.458:E / AndroidRuntime(14966):at java.lang.reflect.Method.invoke(Native Method)   09-16 22:25:38.458:E / AndroidRuntime(14966):at java.lang.reflect.Method.invoke(Method.java:372)   09-16 22:25:38.458:E / AndroidRuntime(14966):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1016)   09-16 22:25:38.458:E / AndroidRuntime(14966):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

2 个答案:

答案 0 :(得分:0)

您已将膨胀的视图分配给v,但您正在使用convertView来查找膨胀的视图窗口小部件。摆脱v并使用convertView

public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       convertView = inflater.inflate(R.layout.drawer, parent, false);
    }

    // Locate the TextViews in drawer_list_item.xml
    TextView title = (TextView) convertView.findViewById(R.id.title);
    // Locate the ImageView in drawer_list_item.xml
    ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
    // Locate the ImageView in drawer_list_item.xml
    ImageView img = (ImageView) convertView.findViewById(R.id.img);

    title.setText(mTitle[position]);
    icon.setImageResource(mIcon[position]);
    img.setImageResource(mimg[position]);

   return convertView;
}

答案 1 :(得分:0)

  

  

     

1 .data
 2 msg1: .word 0:24
 3 .text
 4 .globl main
 5 main:
 6 li $v0, 8 #syscall for read str
 7 la $a0, msg1 #load address of msg1 to store string
 8 li $a1, 100 #msg1 is 100 bytes
 9 syscall