没有适配器;即使布局膨胀,也会跳过布局

时间:2017-10-23 16:31:02

标签: android android-layout

我在Android日志中收到错误“没有附加适配器;跳过布局”,即使布局被数据夸大并且工作正常,我可能是因为代码混乱,有人可以指出我出错的地方在这里,我可以修复它。

注意:应用程序运行正常,没有任何问题,我只是想弄清楚为什么我收到错误没有附加适配器;跳过日志中的布局,也许是因为我在从api获取JSON后调用适配器的方式。

任何见解都会非常有用。

  

MainActivity.java

    public class MainActivity extends AppCompatActivity {

    List<DataAdapter> DataAdapterClassList;

    RecyclerView recyclerView;

    RecyclerView.LayoutManager recyclerViewlayoutManager;

    RecyclerView.Adapter recyclerViewadapter;

    ProgressBar progressBar;

    JsonArrayRequest jsonArrayRequest ;

    ArrayList<String> BankNames;

    RequestQueue requestQueue ;

    String HTTP_SERVER_URL = "http://fyzahmed.com/testapi.php";

    View ChildView ;

    int RecyclerViewClickedItemPOS ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        DataAdapterClassList = new ArrayList<>();

        BankNames = new ArrayList<>();

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);

        progressBar = (ProgressBar) findViewById(R.id.progressBar1);

        recyclerView.setHasFixedSize(true);

        recyclerViewlayoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(recyclerViewlayoutManager);

        // JSON data web call function call from here.
        JSON_WEB_CALL();

        //RecyclerView Item click listener code starts from here.
        recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {

            GestureDetector gestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() {

                @Override public boolean onSingleTapUp(MotionEvent motionEvent) {

                    return true;
                }

            });
            @Override
            public boolean onInterceptTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {

                ChildView = Recyclerview.findChildViewUnder(motionEvent.getX(), motionEvent.getY());

                if(ChildView != null && gestureDetector.onTouchEvent(motionEvent)) {

                    //Getting RecyclerView Clicked item value.
                    RecyclerViewClickedItemPOS = Recyclerview.getChildAdapterPosition(ChildView);

                    //Printing RecyclerView Clicked item clicked value using Toast Message.
                    Toast.makeText(MainActivity.this, BankNames.get(RecyclerViewClickedItemPOS), Toast.LENGTH_LONG).show();

                }

                return false;
            }

            @Override
            public void onTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {

            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        });

    }

    public void JSON_WEB_CALL(){

        progressBar.setVisibility(View.VISIBLE);

        jsonArrayRequest = new JsonArrayRequest(HTTP_SERVER_URL,

                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {

                        JSON_PARSE_DATA_AFTER_WEBCALL(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        requestQueue = Volley.newRequestQueue(this);

        requestQueue.add(jsonArrayRequest);
    }

    public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

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

            DataAdapter GetDataAdapter2 = new DataAdapter();

            JSONObject json = null;
            try {
                json = array.getJSONObject(i);


                GetDataAdapter2.setBankName(json.getString("BANK"));

                //Adding subject name here to show on click event.
                BankNames.add(json.getString("BANK"));

            }
            catch (JSONException e)
            {

                e.printStackTrace();
            }

            DataAdapterClassList.add(GetDataAdapter2);

        }

        progressBar.setVisibility(View.GONE);

        recyclerViewadapter = new RecyclerViewAdapter(DataAdapterClassList, this);

        recyclerView.setAdapter(recyclerViewadapter);
    }
} 

1 个答案:

答案 0 :(得分:0)

  • 您正在使用名为adapter

  • 的方法将JSON_PARSE_DATA_AFTER_WEBCALL绑定到recyclerView
  • 调用方法JSON_PARSE_DATA_AFTER_WEBCALL本身 onResponse()

  • JsonArrayRequest

因此,当您收到响应时,将适配器绑定到RecyclerView的过程(此任务必须花费太长时间才能处理)
这可能是你收到错误的原因

也, 这不是将适配器绑定到recyclerView的好方法 我建议您先创建一个空适配器,然后在获得响应时更新适配器。

希望这可以帮助您理解为什么会出现此错误。