-4

顯示java.lang.NullPointerException:名稱爲null我得到一個空指針異常在我的自定義適配器getView()方法

@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) 

回答

0

您可以getContext()

更換context而且讓您在創建適配器

你必須弄清楚爲什麼iconMapper並不一定不要使用getApplicationContext包含歷史項目類別值

如果全部失敗,則添加一個請檢查設置圖像資源

+0

不....它仍然給我一個空指針異常... –

+0

好吧,就像我說的,添加logcat。我會更新 –

+0

剛做...另外,我檢查了iconmapper的輸出,看起來合法... –

0

將上下文替換爲getContext()或將適當的值傳遞給適配器。 就是這樣。

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

謝謝....我有一個問題,我正在使用的圖標...他們出於某種原因不被接受... –

相關問題