如果我验证这两个项是否为空,我怎么能得到NullException错误?

时间:2017-02-07 20:05:03

标签: android listadapter

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference

执行此代码时,我不断收到此错误:

if (listView != null && adapter != null) {
    listView.setAdapter(adapter);
}

这里是完整的堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference

at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:337)                                                                                            
at android.widget.ListView.setAdapter(ListView.java:491)
at com.themorrisonagency.gcocommuter.RideFragmentPage.setUserVisibleHint(RideFragmentPage.java:94)
at android.support.v4.app.FragmentStatePagerAdapter.setPrimaryItem(FragmentStatePagerAdapter.java:157)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1270)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:277)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:603)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5491)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

以下是我初始化适配器的地方:

public RideMatchesAdapter(Context context, int resource, List<Ride> objects)   
{
   super(context, resource, objects);
   if (objects != null) {
       activityContext = context;
       layoutResource = resource;
       this.rideList = objects;
       final int maxMemory = (int) (Runtime.getRuntime().maxMemory() /1024);
       final int cacheSize = maxMemory / 8;
   } else {
       Log.e(TAG, "objects cannot be null");
   }
}

还尝试检查我的内部列表是否为空。仍然抛出异常:

public int getCount(){
    if(this.rideList != null){
        return this.rideList.size();
    } else {
        return 0;
    }
}

3 个答案:

答案 0 :(得分:2)

查看您添加的单行堆栈跟踪,我的赌注是您的NullPointerException发生在您的适配器代码中。可能在这种方法中:

@Override
public int getItemCount() {
    return itemsList.size();// Maybe your itemsList is null.
}

如果您使用ArrayAdapter,则表示您未正确初始化适配器。您需要将非空列表传递给ArrayAdapter

答案 1 :(得分:1)

我认为来自适配器的异常。检查适配器代码以获取检索数据列表大小的位置。

答案 2 :(得分:0)

感谢您帮助完成此过程。虽然没有任何建议能够解决我的问题,但感谢您的帮助!

我的问题实际上是由适配器引起的!= null,但由于某种原因,我需要重新创建适配器。我认为它会保留,因为碎片没有被破坏。

在我的setUserVisibleHint中:

if (isVisibleToUser) {
        ((RidesActivity) getContext()).setIndicator(POSITION);
        if (fragmentLoaded) {
            if (GACommuter.rideMatches == null) {
                ((RidesActivity) getContext()).loadData();
            } else if(listView != null) {
                adapter = new RideMatchesAdapter(this.getContext(),  R.layout.ride_match, GACommuter.rideMatches);
                adapter.notifyDataSetChanged();
                listView.setAdapter(adapter);
            }
        }
    }