我不断在空对象引用错误中获取com.android.volley.RequestQueue.add

时间:2019-08-15 20:40:32

标签: java android json android-volley

我需要显示JSON文件中的产品列表,但我不断收到此错误:

https://i.imgur.com/WEu4DzV.png

我尝试移动代码中的所有内容(因此也许我将其弄乱了一点),但我无法使其正常工作。我是一个初学者,因此很难为我解决此类错误。

这是产生错误的片段:

package com.example.app;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class ListFragment extends Fragment {

private RecyclerView mRecyclerView;
private ListAdapter mListAdapter;
private ArrayList<Item> mList;
private RequestQueue mRequestQueue;
Context context;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_list, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView = view.findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mListAdapter);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        parseJSON();
        super.onActivityCreated(savedInstanceState);
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mList = new ArrayList<>();
        mListAdapter = new ListAdapter(context, mList);
}


private void parseJSON() {

        String url = "http://my_ip_adress/sestavsisvujsvetweb/api/seznammagnetek";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                int number = 0;
                try {
                    number = response.getInt("monumentid1");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String name = null;
                try {
                    name = response.getString("name1");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String region = null;
                try {
                    region = response.getString("region1");
                } catch (JSONException e) {
                e.printStackTrace();
                }

                mRequestQueue = Volley.newRequestQueue(context);
                mList.add(new Item(number, name, region));

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
        mRequestQueue.add(request); //this causes the problem, why? :(


}

}

1 个答案:

答案 0 :(得分:0)

发生此问题的原因是,您在试图将请求添加到requestQueue之前就知道它已成功检索了您的信息。首先,检查JsonObjectRequest是否具有onSuccess函数,如果没有,则需要在onResponse内部添加到requestQueue。否则,android是同步的,因此它将在实际检索Json之前执行您的代码。

因此快速解决方法是:

if(request != null) {
   mRequestQueue.add(request);
}

如果它为null,将确保不添加。将此添加到您的onCreate中:

mRequestQueue = Volley.newRequestQueue(context);