3

Create three items in the adapter

 3 years ago
source link: https://www.codesd.com/item/create-three-items-in-the-adapter.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Create three items in the adapter

advertisements

I have already make navigation drawer with a ImageView and a TextView,and it can run.However, when I want to creat navigation drawer with two ImageViews and a TextView ,it will show the error("Attempt to invoke virtual method") with my second ImageView(the variable's name in my code is "img")

My code like this:

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

this is my logcat:

09-16 22:25:38.458: E/AndroidRuntime(14966): FATAL EXCEPTION: main

09-16 22:25:38.458: E/AndroidRuntime(14966): java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference 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): at android.widget.AbsListView.obtainView(AbsListView.java:2344) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.widget.ListView.makeAndAddView(ListView.java:1864) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.widget.ListView.fillDown(ListView.java:698) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.widget.ListView.fillFromTop(ListView.java:759) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.widget.ListView.layoutChildren(ListView.java:1673) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.widget.AbsListView.onLayout(AbsListView.java:2148) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.View.layout(View.java:15686) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.ViewGroup.layout(ViewGroup.java:4967) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:914) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.View.layout(View.java:15686) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.ViewGroup.layout(ViewGroup.java:4967) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.widget.FrameLayout.onLayout(FrameLayout.java:508) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.View.layout(View.java:15686) 09-16 22:25:38.458: E/AndroidRuntime(14966): at 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): at android.view.View.layout(View.java:15686) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.ViewGroup.layout(ViewGroup.java:4967) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.widget.FrameLayout.onLayout(FrameLayout.java:508) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.View.layout(View.java:15686) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.ViewGroup.layout(ViewGroup.java:4967) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2076) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1833) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1058) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5796) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.Choreographer.doCallbacks(Choreographer.java:580) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.Choreographer.doFrame(Choreographer.java:550) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.os.Handler.handleCallback(Handler.java:739) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.os.Handler.dispatchMessage(Handler.java:95) 09-16 22:25:38.458: E/AndroidRuntime(14966): at android.os.Looper.loop(Looper.java:211) 09-16 22:25:38.458: E/AndroidRuntime(14966): at 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)


you assigned the inflated view to v, but you are using convertView to look for the inflated views widget. Get rid of v and use just 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;
}




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK