-1

更新現在回收站視圖中不工作

查看加載,我實現了這個之後: -

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

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

,但我仍然可以在登錄此消息: -

E/RecyclerView: No adapter attached; skipping layout 

對此有何想法?


我已經使用抽射從服務器獲取數據,然後

顯示了使用Cardview上回收站視圖中的數據。但它崩潰的應用程序錯誤,說適配器沒有連接。雖然我已經連接了適配器。我無法找到問題。請幫忙。

主要佈局文件

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

主要的Java類

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

我莫代爾類

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 

我無法在這裏找到問題。請幫助

+0

的問題與缺失資源,分不清到底是哪,但它是在缺少行:RecyclerviewAdapter.java:34 - 你想設置一個文本值,但資源你正在設置不存在。 – Booger

回答

1

嘗試改變代碼

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

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

現在加載視圖,但錯誤日誌仍然顯示下面的錯誤。 對此有何想法? E/RecyclerView:沒有連接適配器;跳過佈局 –

+0

@RaviBhardwaj - 那是因爲你的代碼recycler.setAdapter(new RecyclerviewAdapter(Leaderboard.this,users));沒有在應用程序主線程上設置。你可以做的是移動你的代碼setAdapter onCreate和onResponse調用notifyDataStateChanged recyclerview –

1

的問題是在此代碼

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

由於TextView的重載的setText(INT)

應該與字符串資源一起使用的方法,這些資源將通過像R.string。*定義的整數來解決。

在你的情況下用戶。getTime()返回的整型值將被視爲預定義的XML字符串資源(在您的情況下不存在)。

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()可以接受字符串或整型,因爲資源如R.string.somethingint。修復:

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