错误' void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView $ Adapter)'在null对象引用上

时间:2018-01-29 06:41:48

标签: java android android-recyclerview nullpointerexception recycler-adapter

如果我为我的recyclerview分配一个值。我有一个方法" listRe",在那里我获得了idpublication并将其发送到另一种方法" ListFile2"我希望从每个idpublication获取数据。

基本上我想将recyclerview放到另一个回收者视图中。

  

java.lang.NullPointerException:尝试调用虚方法' void   android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView $适配器)'   在null对象引用上                                                      在com.dese.diario.DetailPublication $ 18.onResponse(未知来源)                                                      在com.dese.diario.DetailPublication $ 18.onResponse(未知来源)                                                      在com.android.volley.toolbox.StringRequest.b(未知来源)                                                      在com.android.volley.toolbox.StringRequest.a(未知来源)                                                      在com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(未知   资源)                                                      在android.os.Handler.handleCallback(Handler.java:751)                                                      在android.os.Handler.dispatchMessage(Handler.java:95)                                                      在android.os.Looper.loop(Looper.java:154)                                                      在android.app.ActivityThread.main(ActivityThread.java:6123)                                                      at java.lang.reflect.Method.invoke(Native Method)                                                      在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867)                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

我得到身份证件

 public  void listarRe( final String pa){

        RequestQueue queue = Volley.newRequestQueue(DetailPublication.this);

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

                        //JSONArray jsonArray = null;

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

                            try {
                                listRepublicaciones = new ArrayList<>();
                                JSONArray jsonarray = new JSONArray(response);
                                for (int i = 0; i < jsonarray.length(); i++) {
                                    JSONObject jsonobject = jsonarray.getJSONObject(i);

                                    listRepublicaciones.add(new com.dese.diario.Objects.RePublication(
                                            jsonobject.getString("padre"),
                                            jsonobject.getString("idpublicacion"),
                                            jsonobject.getString("foto"),
                                            jsonobject.getString("observaciones"),
                                            jsonobject.getString("titulo"),
                                            jsonobject.getString("nombre"),
                                            jsonobject.getString("idusuario"),
                                            jsonobject.getString("analisis"),
                                            jsonobject.getString("evaluacion"),
                                            jsonobject.getString("conclusion"),
                                            jsonobject.getString("token"),
                                            jsonobject.getString("planaccion"),
                                            jsonobject.getString("sentimiento")));

                                   /* HashSet hs = new HashSet();

                                    hs.addAll(listRepublicaciones);
                                    listRepublicaciones.clear();*/


                                    adapter = new Adapter_RePubication(listRepublicaciones,DetailPublication.this);
                                    recyclerView.setAdapter(adapter);


                              //   Toast.makeText(DetailPublication.this, "ide re"+jsonobject.getString("idpublicacion").toString(), Toast.LENGTH_LONG).show();


                                  //&  System.out.println(listRepublicaciones);
                                }
                            } catch (JSONException e) {
                                Log.e("DetailPublication", "Error +->" + e);
                            }
                        }
                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Horror", "Response--->"+error);
            }

        }
        ) {
            /**
             * Passing some request headers
             */
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("idpublicacion", pa);
                headers.put("Content-Type", "application/x-www-form-urlencoded");
                return headers;
            }
        };


        queue.add(stringRequest);
    }

我发送了身份证

 public  void listarFile2( final String ide){

        RequestQueue queue = Volley.newRequestQueue(DetailPublication.this);

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

                    //    Toast.makeText(DetailPublication.this,"IDENTF"+ide, Toast.LENGTH_LONG).show();    // paths = new ArrayList<>();


                        //JSONArray jsonArray = null;

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

                            try {

                                JSONArray jsonarray = new JSONArray(response);
                             ///   Toast.makeText(DetailPublication.this,"JSON LENGHT--"+jsonarray.length(), Toast.LENGTH_LONG).show();    // paths = new ArrayList<>();

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

                                    JSONObject jsonobject = jsonarray.getJSONObject(i);
                                    String file=jsonobject.getString("descripcion");
                                    String idre= jsonobject.getString("idpublicacion");
                                    filename.add(file);

                                        Toast.makeText(DetailPublication.this,"IDEPU"+idre+ "Archivos"+ file , Toast.LENGTH_LONG).show();    // paths = new ArrayList<>();

                                        ia = new Adapter_File(filename, DetailPublication.this);

                                            rvItemFeed.setAdapter(ia);
                                            lyContentImagenDetailF.setVisibility(View.VISIBLE);
                                            rvItemFeed.setItemAnimator(new DefaultItemAnimator());
                                            rvItemFeed.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));









                                    //  }

                                }
                            } catch (JSONException e) {
                                Log.e("Detail Publicacion", "Problema con" + e);
                                Toast.makeText(DetailPublication.this, e.toString(), Toast.LENGTH_LONG).show();    // paths = new ArrayList<>();

                            }
                        }
                    }

                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Horror", "Response--->"+error);
            }

        }

        ) {
            /**
             * Passing some request headers
             */
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("idpublicacion", ide);
                //headers.put("Content-Type", "application/x-www-form-urlencoded");
                return headers;
            }
        };
        queue.add(stringRequest);
    }

XML LAYOUT(listarRe)

 <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/recyclerRepost"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentEnd="true"
                        android:layout_alignParentStart="true"
                        android:overScrollMode="never"
                        tools:listitem="@layout/item_repost" />
                    </LinearLayout>

XML LAYOUT 2(listFile2)

 <LinearLayout
            android:id="@+id/lyContentImagenDetailF"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="gone">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="10dp"
                android:background="?colorAccent" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rvItemFeed"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_span="3"
                tools:listitem="@layout/item_file" />

        </LinearLayout>

1 个答案:

答案 0 :(得分:0)

此行生成错误:

recyclerView.setAdapter(adapter);

您是否已将此分配给xml文件中的相应元素?您的代码中是否有类似于以下的行?

recyclerview = findViewById(R.id.recyclerRepost);