Recycler视图无效

时间:2017-10-21 16:22:29

标签: android android-recyclerview android-volley recycler-adapter

更新

现在加载视图,在我实现之后: -

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

E/RecyclerView: No adapter attached; skipping layout

但我仍然在日志中收到此消息: -

<?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"/>

对此有何想法?

我使用volley从服务器获取数据,然后使用Cardview在Recycler视图中显示数据。但它崩溃的应用程序错误说适配器没有附加。虽然我已经连接了适配器。我无法找到问题。请帮助。

主要布局文件

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);
}
}

主Java类

<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>

Cardview

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);
    }
}
}

RecyclerviewAdapter

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;
}
}

我的模态课程

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

我收到错误

JSON

我无法在此处找到问题。请帮助

3 个答案:

答案 0 :(得分:1)

尝试更改代码

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

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

答案 1 :(得分:1)

问题在于此代码

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

由于textview重载了setText(int)

应该与字符串资源一起使用的方法,这些资源将通过定义为R.string的整数来解析。*。

在你的情况下,user.getTime()返回整数值,它将被视为预定义的XML字符串资源(在你的情况下不存在)。

答案 2 :(得分:1)

E/RecyclerView: No adapter attached; skipping layout

这意味着没有适配器,因此将跳过布局,这不是错误信息。如果您不想再看到它,请附加一个空的适配器,然后更新适配器。

这是你的错误:

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

因此:

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

由于:

public Integer getTime() {
    return time;
}

这是一个整数,setText()可以接受String或int,因为R.string.something等资源是int。解决它:

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