活动首次加载时,Volley显示空错误

时间:2018-10-20 09:11:59

标签: android android-studio android-volley

我在android活动中使用了分页器,并使用NodeJs和MongoDb作为后端服务。当我的活动第一次开始时,它显示凌空错误null 当我停止我的应用程序并再次启动它时,它会在视图寻呼机中加载图像并正常工作。 为什么在首次启动活动时它没有从服务器获取数据。

这是我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {

ViewPager viewPager;
TabLayout tab;

RequestQueue rq;
List<Model> list;
Adapter adapter;

String request_url = "https://example.com";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rq = CustomRequest.getInstance(MainActivity.this).getRequestQueue();

    viewPager = findViewById(R.id.viewPager);
    tab = findViewById(R.id.tabLayout);

    list = new ArrayList<>();

    tab.setupWithViewPager(viewPager, true);

    final ProgressDialog prg = new ProgressDialog(MainActivity.this);
    prg.setMessage("Loading");
    prg.show();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, request_url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            prg.dismiss();

            try {
                JSONArray jsonArr  = new JSONArray(response);

                for (int i = 0; i < jsonArr.length(); i++) {

                    Model model = new Model();

                    JSONObject jsonObject = jsonArr.getJSONObject(i);

                    model.setImage(jsonObject.getString("Image"));

                    list.add(model);

                }

                adapter = new Adapter((ArrayList<Model>) list, MainActivity.this);

                viewPager.setAdapter(adapter);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            prg.dismiss();

            Toast.makeText(getApplicationContext(),""+error.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }){

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            HashMap<String,String> params = new HashMap<>();

            params.put("place","Ajmer");

            return params;
        }
    };

    CustomRequest.getInstance(MainActivity.this).addToRequetQueue(stringRequest);
 }
}

Adapter.java

public class Adapter extends PagerAdapter {

ArrayList<Model> list = new ArrayList<>();
Context context;
LayoutInflater layoutInflater;

public Adapter(ArrayList<Model> list, Context context) {
    this.list = list;
    this.context = context;
}

@Override
public int getCount() {
    return list.size();
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
    return view == o;
}

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, final int position) {

    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    assert layoutInflater != null;
    View view = layoutInflater.inflate(R.layout.custom, null);

    Model model =  list.get(position);

    ImageView imageView = view.findViewById(R.id.imageView);

    Glide.with(context).load(model.getImage()).into(imageView);

    ViewPager vp = (ViewPager) container;
    vp.addView(view, 0);
    return view;
}

@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {

    container.removeView((LinearLayout)object);
 }
}

请让我知道为什么我遇到这个问题。

谢谢

1 个答案:

答案 0 :(得分:0)

您是否初始化了RequestQueue。如果没有,请在字符串请求之前添加以下代码

RequestQueue queue = Volley.newRequestQueue(context);

在该调用之后,在字符串请求调用之后添加queue.add

queue.add(stringRequest);

如果错误仍然继续... 截距的打印响应,无论其是否为空。