"RecyclerView:未连接适配器;跳过布局"以供recyclerview使用 [英] “RecyclerView: No Adapter attached; skipping layout” for recyclerview

查看:61
本文介绍了"RecyclerView:未连接适配器;跳过布局"以供recyclerview使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个刚起步的Android开发人员,并且设法将RecyclerView组合在一起.使用Volley解析包含博客文章的json数据,并使用CardViews填充RecyclerView;在设备上运行该错误时,出现未安装适配器;跳过布局"错误.

I am a fledgling Android developer and I have managed to put together a RecyclerView. Using volley to parse json data containing blog posts, and using CardViews to populate the RecyclerView; I am getting this error "No adapter attched; skipping layout" when I run it on my device.

请注意,我已经检查了这个问题 recyclerview跳过布局,但没有帮助.

Please note I have checked this question recyclerview No adapter attached; skipping layout but it didn't help.

MainActivity.java的这一部分

//Creating Views
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;

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

        //Initializing Views
        recyclerView = (RecyclerView) findViewById(R.id.post_recycler);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        //Adding adapter to recyclerview
        recyclerView.setAdapter(adapter);

我也尝试过此操作: MainActivity.java

I have alse tried this: MainActivity.java

    //Creating Views
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;

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

        //Initializing Views
        recyclerView = (RecyclerView) findViewById(R.id.post_recycler);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

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

    //Initializing Views
    recyclerView = (RecyclerView) findViewById(R.id.post_recycler);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);


    //Initializing the postlist list
    mPostItemsList = new ArrayList<>();

    //Caling method to get data
    getData();

//This method will get data from the web api
private void getData(){
    //Showing progress dialog
    final ProgressDialog progressDialog = ProgressDialog.show(this, "Loading posts", "Please wait", false, false);

    //Creating a json request
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(ConfigPost.GET_URL,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    //Dismissing the progress dialog
                    progressDialog.dismiss();

                    //calling method to parse json array
                    parseData(response);

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

                }
            });

    //Creating request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue.add(jsonArrayRequest);
}

//This method will parse json data
private void parseData(JSONArray array){
    for(int i = 0; i<array.length(); i++) {
        PostItems postItem = new PostItems();
        JSONObject jsonObject = null;
        try {
            jsonObject = array.getJSONObject(i);
            postItem.setPost_image(jsonObject.getString(ConfigPost.TAG_POST_IMAGE));
            postItem.setPost_title(jsonObject.getString(ConfigPost.TAG_POST_TITLE));
            postItem.setPost_body(jsonObject.getString(ConfigPost.TAG_POST_BODY));
        } catch (JSONException w) {
            w.printStackTrace();
            //Toast.makeText(this, "Error in parsing Json", Toast.LENGTH_LONG).show();
        }
        mPostItemsList.add(postItem);
    }

    //Finally initialing the adapter
    adapter = new PostAdapter(mPostItemsList, this);

    //Adding adapter to recyclerview
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}

适配器类

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder>{

    private ImageLoader mImageLoader;
    private Context mContext;

    //List of posts
    List<PostItems> mPostItems;



   public PostAdapter(List<PostItems> postItems, Context context) {
       super();

       //Getting all Posts
       this.mPostItems = postItems;
       this.mContext = context;
   }

    @Override
    public ViewHolder  onCreateViewHolder (ViewGroup parent, int viewType) {
        View v =  LayoutInflater.from(parent.getContext())
                .inflate(R.layout.post_summ, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

      PostItems postList = mPostItems.get(position);

      mImageLoader = VolleyRequest.getInstance(mContext).getImageLoader();
      mImageLoader.get(postList.getPost_image(), ImageLoader.getImageListener(holder.postImage, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));

      holder.postImage.setImageUrl(postList.getPost_image(), mImageLoader);
      holder.postTitle.setText(postList.getPost_title());
      holder.postBody.setText(postList.getPost_body());

    }


    @Override
    public int getItemCount(){
        //Return the number of items in the data set
        return mPostItems.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        public NetworkImageView postImage;
        public TextView postTitle;
        public TextView postBody;

        public ViewHolder(View postView) {
            super(postView);
            postImage = (NetworkImageView) postView.findViewById(R.id.post_image);
            postTitle = (TextView) postView.findViewById(R.id.post_title);
            postBody = (TextView) postView.findViewById(R.id.post_body);
        }
    }
}

XML布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.jadebyte.geekng.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/post_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"/>
</RelativeLayout>

任何想法可能是什么原因造成的?

Any Idea what might be causing it?

推荐答案

我认为您传递给适配器的数据有问题. 我用您的代码做了一个简单的例子,但没有任何问题. 请检查一次 https://www.dropbox.com/s/xcw6vm3js1ue9ja/SamplePro_Example.zip ?dl = 0

I think something wrong with data which you are passing to adapter. I made a simple example with your code but I didn't get any issue. Please check this once https://www.dropbox.com/s/xcw6vm3js1ue9ja/SamplePro_Example.zip?dl=0

也请粘贴您的post_summ.xml代码.这样我就可以验证更多

And also please paste your post_summ.xml code. So that I can verify more

这篇关于"RecyclerView:未连接适配器;跳过布局"以供recyclerview使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆