我在自定义适配器getView()方法中得到一个空指针异常

时间:2016-11-11 02:28:22

标签: android listview nullpointerexception android-resources listadapter

java.lang.NullPointerException:name为null

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

    Holder holder;
    if (convertView == null) {
        holder = new Holder();
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.history_item, parent, false);

        holder.tvCat = (TextView)convertView.findViewById(R.id.tv_category);
        holder.tvAmt = (TextView)convertView.findViewById(R.id.tv_amount);
        holder.tvDate = (TextView)convertView.findViewById(R.id.tv_date);
        holder.img = (ImageView)convertView.findViewById(R.id.iv);
        convertView.setTag(holder);

    }

    holder=(Holder)convertView.getTag();
    //Holder holder = new Holder();
    //View rowView;
    HistoryItem historyItem;
    //rowView=inflater.inflate(R.layout.history_item,null);

    historyItem = historyItems.get(position);

    holder.tvCat.setText(historyItem.getCategory());
    holder.tvAmt.setText(historyItem.getAmount()+"");

    DateFormat df = new SimpleDateFormat("yyyy/MM/dd kk:mm:ss");
    Calendar c    = Calendar.getInstance();
    c.setTimeInMillis(historyItem.getTimestamp());
    Date day      = c.getTime();

    holder.tvDate.setText(df.format(day));

    Resources res = context.getResources();
    String mDrawableName = iconMapper.get(historyItem.getCategory());

    //This line is giving the null pointer exception...
    int resID = res.getIdentifier(mDrawableName , "drawable", context.getPackageName());

    holder.img.setImageResource(resID);

    return convertView;
}

我检查了mDrawableName,它保存了正确的值 此外,上下文来自我的ListView类 任何帮助将不胜感激。

Logcat:

java.lang.NullPointerException: name is null
                                                                                   at android.content.res.ResourcesImpl.getIdentifier(ResourcesImpl.java:214)
                                                                                   at android.content.res.Resources.getIdentifier(Resources.java:1866)
                                                                                   at com.example.sharang.wheresmymoney.HistoryCustomAdapter.getView(HistoryCustomAdapter.java:92)
                                                                                   at com.baoyz.swipemenulistview.SwipeMenuAdapter.getView(SwipeMenuAdapter.java:49)
                                                                                   at android.widget.AbsListView.obtainView(AbsListView.java:2369)
                                                                                   at android.widget.ListView.makeAndAddView(ListView.java:1970)
                                                                                   at android.widget.ListView.fillDown(ListView.java:704)
                                                                                   at android.widget.ListView.fillFromTop(ListView.java:765)
                                                                                   at android.widget.ListView.layoutChildren(ListView.java:1744)
                                                                                   at android.widget.AbsListView.onLayout(AbsListView.java:2161)
                                                                                   at android.view.View.layout(View.java:17526)
                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                                                   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                                   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                                   at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                                   at android.view.View.layout(View.java:17526)
                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                                                   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                                   at android.view.View.layout(View.java:17526)
                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                                                   at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:433)
                                                                                   at android.view.View.layout(View.java:17526)
                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                                                   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                                   at android.view.View.layout(View.java:17526)
                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                                                   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                                   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                                   at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                                   at android.view.View.layout(View.java:17526)
                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                                                   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                                   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                                   at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
                                                                                   at android.view.View.layout(View.java:17526)
                                                                                   at android.view.ViewGroup.layout(ViewGroup.java:5612)
                                                                                   at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
                                                                                   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
                                                                                   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
                                                                                   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6307)
                                                                                   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                                                                                   at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                                                                                   at android.view.Choreographer.doFrame(Choreographer.java:619)
                                                                                   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                                                                                   at android.os.Handler.handleCallback(Handler.java:751)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

2 个答案:

答案 0 :(得分:0)

您可以将context替换为getContext()

确保在创建适配器时不使用getApplicationContext

您必须弄清楚为什么iconMapper不包含historyItem类别值

如果全部失败,则在设置图像资源

周围添加空检查

答案 1 :(得分:0)

用getContext()替换上下文或将上下文传递给适配器。 这样的事情。

 public Adapter(Context context, boolean isGrid, ArrayList<Recipe> data){
    this.context = context;
    this.recipes = data;
    this.isGrid = isGrid;
}