尝试将ImageView放入适配器

时间:2016-08-07 18:05:26

标签: android nullpointerexception imageview android-arrayadapter

我试图将图片视图放在适配器中。

我试图这样做是因为我看到了将一个 TextView 放在ArrayAdapter中的示例,如下所示:

  adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, transmit_noktp_list) {
         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);         
            TextView text = (TextView) view.findViewById(android.R.id.text1);     
            return view;
         }
      };

从示例中得出,然后我尝试将 ImageView 也用于实现icon&amp;每个列表项上的文字:

试了一下:

  adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, transmit_noktp_list) {
         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            ImageView img = (ImageView) view.findViewById(android.R.id.icon1);
            TextView text = (TextView) view.findViewById(android.R.id.text1);

            Resources resources = getResources();
            img.setImageDrawable(resources.getDrawable(R.drawable.inbox));

            return view;
         }
      };

令人惊讶的是,这条 img.setImageDrawable(resources.getDrawable(R.drawable.inbox)); 行给我一个例外(我可以保证&#39; 收件箱 &#39;图标位于res/drawable,它甚至显示在我的IDE中:

  

E / AndroidRuntime:致命异常:主要                                                                                           过程:com.androidbelieve.drawerwithswipetabs,PID:24855                                                                                           显示java.lang.NullPointerException                                                                                               在   com.androidbelieve.drawerwithswipetabs.DaftarAspirasiFragment $ 1.getView(DaftarAspirasiFragment.java:55)                                                                                               在android.widget.AbsListView.obtainView(AbsListView.java:2263)                                                                                               在android.widget.ListView.makeAndAddView(ListView.java:1790)                                                                                               在android.widget.ListView.fillDown(ListView.java:691)                                                                                               在android.widget.ListView.fillFromTop(ListView.java:752)                                                                                               在android.widget.ListView.layoutChildren(ListView.java:1616)                                                                                               在android.widget.AbsListView.onLayout(AbsListView.java:2091)                                                                                               在android.view.View.layout(View.java:14817)                                                                                               在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                               在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)                                                                                               在android.view.View.layout(View.java:14817)                                                                                               在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                               在android.support.v4.view.ViewPager.onLayout(ViewPager.java:1695)                                                                                               在android.view.View.layout(View.java:14817)                                                                                               在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                               在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)                                                                                               在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)                                                                                               在android.widget.LinearLayout.onLayout(LinearLayout.java:1434)                                                                                               在android.view.View.layout(View.java:14817)                                                                                               在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                               在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)                                                                                               在android.widget.FrameLayout.onLayout(FrameLayout.java:388)                                                                                               在android.view.View.layout(View.java:14817)                                                                                               在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                               在   android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1187)                                                                                               在android.view.View.layout(View.java:14817)                                                                                               在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                               在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)                                                                                               在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)                                                                                               在android.widget.LinearLayout.onLayout(LinearLayout.java:1434)                                                                                               在android.view.View.layout(View.java:14817)                                                                                               在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                               在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)                                                                                               在android.widget.FrameLayout.onLayout(FrameLayout.java:388)                                                                                               在android.view.View.layout(View.java:14817)                                                                                               在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                               在   android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)                                                                                               在android.view.View.layout(View.java:14817)                                                                                               在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                               在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)                                                                                               在android.widget.FrameLayout.onLayout(FrameLayout.java:388)                                                                                               在android.view.View.layout(View.java:14817)                                                                                               在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                               在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)                                                                                               在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)                                                                                               在android.widget.LinearLayout.onLayout(LinearLayout.java:1434)                                                                                               在android.view.View.layout(View.java:14817)                                                                                               在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                               在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)                                                                                               在android.widget.FrameLayout.onLayout(FrameLayout.java:388)                                                                                               在android.view.View.layout(View.java:14817)                                                                                               在android.view.ViewGroup.layout(ViewGroup.java:4631)                                                                                               在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2011)                                                                                               在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1768)                                                                                               在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1024)                                                                                               在   android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5694)                                                                                               在   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761)                                                                                               在android.view.Choreographer.doCallbacks(Choreographer.java:574)                                                                                               在android.view.Choreographer.doFrame(Choreographer.java:544)                                                                                               在   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747)                                                                                               在android.os.Handler.handleCallback(Handler.java:733)                                                                                               在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                               在android.os.Looper.loop(Looper.java:136)                                                                                               在android.app.ActivityThread.main(ActivityThread.java:5045)                                                                                             在java.lang.reflect。

1 个答案:

答案 0 :(得分:0)

而不是

 img.setImageDrawable(resources.getDrawable(R.drawable.inbox));

尝试img.setImageResource(R.drawable.inbox);