ListAdapter为null

时间:2018-01-15 15:26:21

标签: java android

我正在尝试使用ListView来填充ListAdapter,但每次创建片段时,都会在列表应添加项目的最后一行上抛出NullPointerException

    @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    list = (ListView) getActivity().findViewById(R.id.list);

    JSONObject jsonObject1 = null;
    JSONObject jsonObject2 = null;
    JSONArray jsonArray1 = null;
    JSONArray jsonArray2 = null;

    try
    {
        jsonObject1 = new JSONObject(MainActivity.dispensaries);
        jsonArray1 = jsonObject1.optJSONArray("dispensaries");
        for(int i = 0 ; i<jsonArray1.length() ; i++)
        {
            try {
                JSONObject object = jsonArray1.getJSONObject(i);
                if(MainActivity.ID.equals(object.get("dispensary_id")))
                {
                    jsonObject2 = object;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        jsonArray2 = jsonObject2.optJSONArray("dispensary_products");
        Log.e(TAG , "jsonArray = " + jsonArray2);
    }
    catch(JSONException e)
    {
        e.printStackTrace();
    }
    for(int i = 0 ; i<jsonArray2.length() ; i++)
    {
        try {

                JSONObject object = jsonArray2.getJSONObject(i);
                if(object.has("product_id")) {
                    names.add(object);
                }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    Log.e(TAG , "list = " + names);
    adapter = new DispensaryMenuAdapter(getContext(), names);
    list.setAdapter(adapter); //here is where the exception is thrown


}

}

以下是例外文字:

    java.lang.NullPointerException: Attempt to invoke a virtual method on a null object reference
                  at com.example.dragosandrei.greenkinexdorian.DispensaryMenuFragment.onViewCreated(DispensaryMenuFragment.java:95)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1314)
                  at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                  at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                  at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                  at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                  at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1984)
                  at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:626)
                  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                  at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
                  at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
                  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
                  at android.view.View.measure(View.java:19917)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
                  at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
                  at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
                  at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1391)
                  at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784)
                  at android.view.View.measure(View.java:19917)
                  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                  at android.view.View.measure(View.java:19917)
                  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                  at android.view.View.measure(View.java:19917)
                  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                  at android.view.View.measure(View.java:19917)
                  at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
                  at android.view.View.measure(View.java:19917)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                  at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                  at android.view.View.measure(View.java:19917)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1485)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:775)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:657)
                  at android.view.View.measure(View.java:19917)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                  at android.view.View.measure(View.java:19917)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1485)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:775)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:657)
                  at android.view.View.measure(View.java:19917)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6139)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
E/AndroidRuntime:     at com.android.internal.policy.DecorView.onMeasure(DecorView.java:729)
                  at android.view.View.measure(View.java:19917)
                  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2436)
                  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1482)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1741)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1366)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6768)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:926)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:735)
                  at android.view.Choreographer.doFrame(Choreographer.java:667)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:912)
                  at android.os.Handler.handleCallback(Handler.java:761)
                  at android.os.Handler.dispatchMessage(Handler.java:98)
                  at android.os.Looper.loop(Looper.java:156)
                  at android.app.ActivityThread.main(ActivityThread.java:6523)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

这是XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@+id/dispensaryMenuList"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_weight="1"
    android:scrollbarSize="3dp"
    android:scrollbarStyle="outsideOverlay"
    android:scrollbars="vertical"
    android:scrollingCache="true"
    android:layout_marginBottom="50sp"
    android:smoothScrollbar="true" />
</RelativeLayout>

如果您需要ListAdapter's代码

,请在评论中注明

1 个答案:

答案 0 :(得分:2)

问题在于这一行:

list = (ListView) getActivity().findViewById(R.id.list);

您不应该从您的片段中调用getActivity().findViewById。要从Fragments布局获取视图,请使用以下命令:

list = (ListView) view.findViewById(R.id.list);