Recycler view not working

-1

Update

View is loaded now, after i implemented this :-

holder.time.setText(user.getTime());

to

holder.time.setText(Integer.toString(user.getTime()));

but I still get this message in Log :-

E/RecyclerView: No adapter attached; skipping layout

any idea about this ?


I have used volley to get data from server and then show the data on Recycler view using Cardview. But it is crashing the app with error saying that adapter not attached. Although I have attached the adapter. I am not able to find the issue. Please Help.

Main Layout File

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycle"/>

Main Java Class

public class Leaderboard extends AppCompatActivity {

private static final String URL = "URL";

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

    final RecyclerView recycler = (RecyclerView) findViewById(R.id.recycle);
    recycler.setLayoutManager(new LinearLayoutManager(Leaderboard.this));

    StringRequest request = new StringRequest(URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.i("TAG",response);
            GsonBuilder gsonBuilder = new GsonBuilder();
            Gson gson = gsonBuilder.create();
            User[] users = gson.fromJson(response, User[].class);
            recycler.setAdapter(new RecyclerviewAdapter(Leaderboard.this,users));
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(Leaderboard.this, "Something Went Wrong!", Toast.LENGTH_SHORT).show();
        }
    });

    RequestQueue queue = Volley.newRequestQueue(this);
    queue.add(request);
}
}

Cardview

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:textSize="24dp"
        android:padding="8dp"
        android:textStyle="bold"
        android:text="1."
        android:gravity="center"
        android:id="@+id/rank"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="6"
        android:textSize="24dp"
        android:padding="8dp"
        android:textStyle="bold"
        android:text="Ravi Bhardwaj"
        android:id="@+id/name"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:textSize="24dp"
        android:padding="8dp"
        android:textStyle="bold"
        android:gravity="center"
        android:text="00:04:10"
        android:id="@+id/time"/>
</LinearLayout>

RecyclerviewAdapter

public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.RecyclerViewHolder> {

private Context context;
private User[] data;
public RecyclerviewAdapter(Context context, User[] data){
    this.context = context;
    this.data = data;
}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.one_row_leaderboard,parent,false);
    return new RecyclerViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
    User user = data[position];
    holder.name.setText(user.getName());
    holder.time.setText(user.getTime());
   // holder.rank.setText(""+data[position+1]);
}

@Override
public int getItemCount() {
    return data.length;
}

public class RecyclerViewHolder extends RecyclerView.ViewHolder{
    TextView rank,name,time;
    public RecyclerViewHolder(View itemView) {
        super(itemView);
        rank = (TextView) itemView.findViewById(R.id.rank);
        name = (TextView) itemView.findViewById(R.id.name);
        time = (TextView) itemView.findViewById(R.id.time);
    }
}
}

My Modal Class

public class User {

@SerializedName("name")
@Expose
private String name;
@SerializedName("time")
@Expose
private Integer time;
@SerializedName("id")
@Expose
private String id;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Integer getTime() {
    return time;
}

public void setTime(Integer time) {
    this.time = time;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}
}

Error I Get

E/RecyclerView: No adapter attached; skipping layout
10-21 21:38:15.160 6718-6718/com.ravibhardwaj.kbcmaster I/TAG: [{"name":"sm","time":240,"id":"19760377672"},{"name":"Ravi Bhardwaj","time":246,"id":"1976037352655019"},{"name":"Ravi Bhard","time":356,"id":"1976037352655566"},{"name":"Ravi Bhard","time":555,"id":"1976037352655566"}]
10-21 21:38:15.255 6718-6718/com.ravibhardwaj.kbcmaster W/ResourceType: No package identifier when getting value for resource number 0x000000f0
10-21 21:38:15.259 6718-6718/com.ravibhardwaj.kbcmaster E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.ravibhardwaj.kbcmaster, PID: 6718
                                                                      android.content.res.Resources$NotFoundException: String resource ID #0xf0
                                                                          at android.content.res.HwResources.getText(HwResources.java:446)
                                                                          at android.widget.TextView.setText(TextView.java:4660)
                                                                          at com.ravibhardwaj.kbcmaster.RecyclerviewAdapter.onBindViewHolder(RecyclerviewAdapter.java:34)
                                                                          at com.ravibhardwaj.kbcmaster.RecyclerviewAdapter.onBindViewHolder(RecyclerviewAdapter.java:14)
                                                                          at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6356)
                                                                          at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6389)
                                                                          at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5335)
                                                                          at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)
                                                                          at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
                                                                          at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
                                                                          at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
                                                                          at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
                                                                          at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
                                                                          at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
                                                                          at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
                                                                          at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)
                                                                          at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)
                                                                          at android.view.View.layout(View.java:17694)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5631)
                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                          at android.view.View.layout(View.java:17694)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5631)
                                                                          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1766)
                                                                          at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1610)
                                                                          at android.widget.LinearLayout.onLayout(LinearLayout.java:1519)
                                                                          at android.view.View.layout(View.java:17694)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5631)
                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                          at android.view.View.layout(View.java:17694)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5631)
                                                                          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1766)
                                                                          at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1610)
                                                                          at android.widget.LinearLayout.onLayout(LinearLayout.java:1519)
                                                                          at android.view.View.layout(View.java:17694)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5631)
                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                          at com.android.internal.policy.DecorView.onLayout(DecorView.java:774)
                                                                          at android.view.View.layout(View.java:17694)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5631)
                                                                          at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2513)
                                                                          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2228)
                                                                          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1366)
                                                                          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6768)
                                                                          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:926)
                                                                          at android.view.Choreographer.doCallbacks(Choreographer.java:735)
                                                                          at android.view.Choreographer.doFrame(Choreographer.java:667)
                                                                          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:912)
                                                                          at android.os.Handler.handleCallback(Handler.java:761)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                          at android.os.Looper.loop(Looper.java:156)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6577)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
10-21 21:38:15.259 6718-6718/com.ravibhardwaj.kbcmaster E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
10-21 21:38:15.272 6718-6718/com.ravibhardwaj.kbcmaster I/Process: Sending signal. PID: 6718 SIG: 9

I am not able to locate the issue in here. Please help

android
android-volley
android-recyclerview
asked on Stack Overflow Oct 21, 2017 by Ravi Bhardwaj • edited Oct 21, 2017 by Ravi Bhardwaj

3 Answers

1

try changing code

holder.time.setText(user.getTime());

to

holder.time.setText(Integer.toString(user.getTime()));
answered on Stack Overflow Oct 21, 2017 by Amod Gokhale
1

The problem is in this code

holder.time.setText(user.getTime());

As textview have overloaded setText(int)

Method which supposed to be used with string resources, these resources will be resolved by integer number defined like R.string.* .

In your case user.getTime() returning integer value that will be treated like a predefined XML string resource(that will not exist in your case).

answered on Stack Overflow Oct 21, 2017 by Alok
1
E/RecyclerView: No adapter attached; skipping layout

That means there is no adapter, therefore the layout will be skipped, that is not the error is info. If you don't want to see it anymore, then attach an empty adapter and then update the adapter.

This is your error:

 E/AndroidRuntime: FATAL EXCEPTION: main
                                  Process: com.ravibhardwaj.kbcmaster, PID: 6718
                                                                      android.content.res.Resources$NotFoundException: String resource ID #0xf0

Because of this:

holder.time.setText(user.getTime());

Because:

public Integer getTime() {
    return time;
}

That is an integer, setText() can accept String or int, because of resources such as R.string.something is an int. Fix it:

holder.time.setText(String.valueOf(user.getTime()));
answered on Stack Overflow Oct 21, 2017 by cutiko

User contributions licensed under CC BY-SA 3.0