Home > Software engineering >  This is not a duplicate, having a different E/RecyclerView: No layout manager attached; skipping lay
This is not a duplicate, having a different E/RecyclerView: No layout manager attached; skipping lay

Time:01-02

I have tried everything including adding

implementation 'com.android.support:recyclerview-v7:27.0.0'

These are my related java classes

Home.java

package com.example.re_search;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.re_search.model.PostModel;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class Home extends Fragment{


    private ArrayList<Integer> pkPost = new ArrayList<>();
    private ArrayList<String> namePost = new ArrayList<>();
    private RecyclerView recyclerView;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.post_list, container, false);

        recyclerView = rootView.findViewById(R.id.recycler_category_list);



        if ( InternetUtil.isInternetOnline(getActivity()) ){
            ClearList();
            showAllPosts();
        }


        getActivity().setTitle("Clusters");

        return rootView;


    }

    private void showAllPosts() {

        Log.i("All posts showed", "All posts showed");
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(PostApi.API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Log.i("Retrofit build", "Retrofit build");
        PostApi postApi= retrofit.create(PostApi.class);
        Log.i("Postapi createdd", "Postapi createdd");
        Call<List<PostModel>> call = postApi.getListPost();
        Log.i("Call list called", "Call list called");

        call.enqueue(new Callback<List<PostModel>>() {

            @Override
            public void onResponse(Call<List<PostModel>> call, Response<List<PostModel>> response) {

                Log.i("Response Received", "Response Received");
                if(response.isSuccessful()){

                    Log.i("Response Received", "Response Received");
                    if (response.body() != null) {
                        List<PostModel> postList = response.body();

                        for(PostModel h:postList){

                            //Integer cat_id = h.getId();
                            //pkPost.add(cat_id);

                            String cat_name = h.getTitle();
                            namePost.add(cat_name);


                        }

                        initRecyclerView();

                    }

                }else {
                    Log.d("fail", "fail");
                }
            }

            @Override
            public void onFailure(Call<List<PostModel>> call, Throwable t) {
                Log.d("fail", t.getMessage() == null ? "" : t.getMessage());
            }

        });

    }


    private void initRecyclerView(){
        Log.d("Home", "initRecyclerView: init recyclerview.");
        RecyclerHomeList adapter = new RecyclerHomeList(getActivity(), pkPost, namePost);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }


    public void ClearList()
    {
        pkPost.clear();
        namePost.clear();

        RecyclerHomeList adapter = new RecyclerHomeList(getActivity(), pkPost,  namePost);
        adapter.notifyDataSetChanged();
        recyclerView.setAdapter(adapter);
    }





    @Override
    public void onResume() {

        super.onResume();

        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
                    getActivity().finish();
                    return true;
                }
                return false;
            }
        });
    }




}

RecyclerHomeList.java

package com.example.re_search;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;




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

    private static final String TAG = "RecyclerProfileListAdapter";

    private ArrayList<Integer> pkPost = new ArrayList<>();
    private ArrayList<String> namePost = new ArrayList<>();


    private Context mContext;

    public RecyclerHomeList(Context context, ArrayList<Integer> rId, ArrayList<String> rName) {
        pkPost = rId;
        namePost = rName;

        mContext = context;
    }

    @Override
    public RecyclerHomeList.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_list_item, parent, false);
        RecyclerHomeList.ViewHolder holder = new RecyclerHomeList.ViewHolder(view);
        return holder;
    }

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


        holder.TId.setText(String.valueOf(pkPost.get(position)));
        holder.TName.setText(namePost.get(position));

        holder.parentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



//                Bundle bundle = new Bundle();
//                bundle.putInt("post_id",pkPost.get(position)); // Put anything what you want
//                AppCompatActivity activity = (AppCompatActivity) view.getContext();
//                Fragment myFragment = new ShowPost();
//                myFragment.setArguments(bundle);
//                activity.getSupportFragmentManager()
//                        .beginTransaction()
//                        .replace(R.id.fragment_container, myFragment)
//                        .addToBackStack(null)
//                        .commit();



            }
        });
    }

    @Override
    public int getItemCount() {
        return pkPost.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder{

        TextView TId;
        TextView TName;

        RelativeLayout parentLayout;

        public ViewHolder(View itemView) {
            super(itemView);

            TId = itemView.findViewById(R.id.post_listitem_id);
            TName = itemView.findViewById(R.id.post_listitem_name);


            parentLayout = itemView.findViewById(R.id.parent_layout);
        }
    }


}

Related XML Files:

post_list.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >


        <RelativeLayout
            android:id="@ id/footer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_gravity="center"
            android:gravity="center">



            <fragment
                android:id="@ id/button_fragment1"
                android:name="com.example.re_search.Footer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />



        </RelativeLayout>

        <LinearLayout
            android:id="@ id/scroll_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@ id/footer"

            android:fillViewport="true">


            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#F3F3F3"
                android:orientation="vertical">


                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@ id/recycler_category_list">
                </android.support.v7.widget.RecyclerView>


            </LinearLayout>


        </LinearLayout>
    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

post_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@ id/parent_layout">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginRight="16dp"
        android:orientation="vertical"
        android:background="#ffffff">



        <LinearLayout
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_marginRight="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="9dp"
            android:layout_marginBottom="10dp"
            >

            <TextView
                android:id="@ id/post_listitem_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""

                />
            <TextView
                android:id="@ id/post_listitem_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                />


        </LinearLayout>



    </LinearLayout>






</RelativeLayout>

This is the error trace

I/TetheringManager: registerTetheringEventCallback:com.example.re_search
W/ample.re_searc: Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (greylist,core-platform-api, reflection, allowed)
    Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (greylist,core-platform-api, reflection, allowed)
    Accessing hidden method Ldalvik/system/CloseGuard;->warnIfOpen()V (greylist,core-platform-api, reflection, allowed)
I/Response Received: Response Received
I/Token: 84639f11977c3078fa92d4f7abc0cb316ef91847
D/CompatibilityChangeReporter: Compat change id reported: 147798919; UID 10121; state: DISABLED
I/All posts showed: All posts showed
I/Retrofit build: Retrofit build
I/Postapi createdd: Postapi createdd
I/Call list called: Call list called
E/RecyclerView: No layout manager attached; skipping layout
I/Response Received: Response Received
    Response Received
D/Home: initRecyclerView: init recyclerview.

I am trying to send a get response to the django server but this error is killing my app after the login. please help please.

This is my grade file

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.re_search"
        minSdkVersion 14
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.android.support:recyclerview-v7:27.0.0'
    //implementation 'com.android.support.constraint:constraint-layout:1.0.2'
}

CodePudding user response:

You need to set your adapter after you have set your LayoutManager.

Change from this:

recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

to this:

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);

CodePudding user response:

The call to initRecyclerView() is done on a background thread, that's the issue. Make the call inside of Oncreate and update values using adapter.datasetchanged()-methods.

CodePudding user response:

You're getting this error because you're initializing your RecyclerView from an asynchronous callback.

You should initialize your RecyclerView inside onCreateView():

// Set up your RecyclerView with the appropriate Layout Manager
RecyclerView myRecycler = findViewById(R.id.my_recycler_id);
myRecycler.setLayoutManager(new LinearLayoutManager(this));

// Create your data set
myData = new ArrayList<MyDataType>();

// Create an instance of your adapter passing the data set into the constructor
myAdapter = new MyAdapter(this, myData);

// Set the Adapter on the RecyclerView directly within onCreate
// so that it doesn't get skipped
myRecycler.setAdapter(myAdapter);

Inside of your Adapter class, create a function to update your data set:

public void updateData(ArrayList<MyDataType> newDataSet){
    myAdapterDataSet = newDataSet;

    // Let the Adapter know the data has changed and the view should be refreshed
    notifyDataSetChanged();
}

Inside of your asynchronous callback, update your adapter:

@Override
public void onDataChange(DataSnapshot snapshot){
    // Add the new data to your data set ex. myData.add(newData)
    // ...

    // After adding to the data set,
    // update the data using a custom function you define in your Adapter's class
    myAdapter.updateData(myData);
}
  • Related