列表视图中的Json Null对象引用

时间:2015-05-26 15:24:40

标签: java android json listview android-fragments

我在ListView中有一个Fragment,我想显示当前列表JSON来电的响应。

问题是当我检查列表时,除了onCreateView以外,ListView工作的值都是正确的。

我的错误是:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at com.example.paer.agileproject.fragments.PTStoryFragment.parseRespon(PTStoryFragment.java:148)
        at com.example.paer.agileproject.fragments.PTStoryFragment$1.onResponse(PTStoryFragment.java:75)
        at com.example.paer.agileproject.fragments.PTStoryFragment$1.onResponse(PTStoryFragment.java:71)
        at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

片段:

String urlProject = "https://www.pivotaltracker.com/services/v5/projects/";

ListView currentView;
ArrayList<String> currentList = new ArrayList<String>();
ArrayList<String> currentItems = new ArrayList<>();
ArrayAdapter<String> currentAdapter;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    View view = inflater.inflate(R.layout.fragment_pivotal_tab, container, false);
    ViewPager viewPager = (ViewPager) view.findViewById(R.id.pivotal_viewpager);
    viewPager.setAdapter(new PivotalFragmentPagerAdapter(getFragmentManager()));
    PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) view.findViewById(R.id.pivotal_tab);
    tabsStrip.setViewPager(viewPager);
    currentView = (ListView) view.findViewById(R.id.ptCurrentList);
    currentAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, currentItems);
    displayJsonResponseProject();
    return view;
}

void displayJsonResponseProject() {
    String url = urlProject + PivotalTrackerFragment.choosenProjectId + "/stories?token=" + PivotalTrackerFragment.token;
    RequestQueue queue = Volley.newRequestQueue(getActivity());
    System.out.println(url);
    JsonArrayRequest getRequest = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>()
            {
                @Override public void onResponse(JSONArray response) {
                    parseRespon(response);
                    System.out.println("1" + currentList.toString());
                    currentView.setAdapter(currentAdapter);
                }
            },
            new Response.ErrorListener()
            {
                @Override public void onErrorResponse(VolleyError error) {
                    Log.d("Error.Response", error.toString());
                }
            }
    );
    queue.add(getRequest);
}

void parseRespon(JSONArray response){
    try {
        Story temp = new Story();
        for(int i = 0;i < response.length(); i++){
            JSONObject story  = response.getJSONObject(i);
            if(story.getString("kind") == null)
                temp.setKind(null);
            else
                temp.setKind(story.getString("kind"));
            if(story.getString("id") == null)
                temp.setId(null);
            else
                temp.setId(story.getString("id"));
            if (story.getString("created_at") == null)
                temp.setCreateAt(null);
            else
                temp.setCreateAt(story.getString("created_at"));
            if (story.getString("updated_at") == null)
                temp.setUpdateAt(null);
            else
                temp.setUpdateAt(story.getString("updated_at"));
            //if (story.getString("accepted_at") == null)
            //     temp.setAcceptedAt(null);
            // else
            //     temp.setAcceptedAt(story.getString("accepted_at"));
            if (story.getString("estimate") == null)
                temp.setEstimate("No point.");
            else
                temp.setEstimate(story.getString("estimate"));
            if (story.getString("story_type") == null)
                temp.setStoryType(null);
            else
                temp.setStoryType(story.getString("story_type"));
            if (story.getString("name") == null)
                temp.setName(null);
            else
                temp.setName(story.getString("name"));
            if (story.getString("current_state") == null )
                temp.setCurrentState(null);
            else
                temp.setCurrentState(story.getString("current_state"));
            if (story.getString("url") == null)
                temp.setUrl(null);
            else
                temp.setUrl(story.getString("url"));
            if (story.getString("owner_ids") == null)
                temp.setOwnedById(null);
            else

                temp.setOwnedById(story.getString("owner_ids"));
            /*if (story.getString("description") != null)
                temp.setDescriptionescription(story.getString("description"));
            else
                temp.setDescriptionescription(null);
            storylist.add(temp);*/
            currentList.add(temp.getName());
        }
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
}
}

2 个答案:

答案 0 :(得分:0)

请尝试添加您的行

currentAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, currentItems);

行[{1}}

之前

答案 1 :(得分:0)

onCreateView

...
currentView = (ListView) view.findViewById(R.id.ptCurrentList);
currentAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, currentItems);
// set up adapter on currentView Here
currentView.setAdapter(currentAdapter);
displayJsonResponseProject();
...

并在getRequest

    ...
    JsonArrayRequest getRequest = new JsonArrayRequest(url,
        new Response.Listener<JSONArray>()
        {
            @Override public void onResponse(JSONArray response) {
                parseRespon(response);
                System.out.println("1" + currentList.toString());
                // insted of setting up adapter here, 
                // just call Adapter and tell that data is changed.
                currentAdapter.notifyDataSetChanged();
            }
        }
    ...
  

另外请确保R.id.ptCurrentList内有R.layout.fragment_pivotal_tab。我想这可能是你的问题。