我收到以下错误:
android.content.res.Resources$NotFoundException: String resource ID #0x0
我只是使用创建一个号码列表RecyclerView
:
import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class SongRVAdapter extends RecyclerView.Adapter{ private Context context; public SongRVAdapter(Context context) { this.context = context; } @Override public SongViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.song_info_row, parent, false); return new SongViewHolder(view); } @Override public void onBindViewHolder(SongViewHolder holder, int position) { Log.d("CodeKamp", "onBindViewHolder called for position " + position); holder.titleTextView.setText(position); holder.durationTextView.setText("00:" + position); } @Override public int getItemCount() { return 10; } }
Michael Dodd.. 6
您不能将int
值传递给setText()
,否则它将尝试查找与该int
ID 匹配的String资源。所以
holder.titleTextView.setText(position);
需要更改为
holder.titleTextView.setText(String.valueOf(position));
转换position
为字符串。
您不能将int
值传递给setText()
,否则它将尝试查找与该int
ID 匹配的String资源。所以
holder.titleTextView.setText(position);
需要更改为
holder.titleTextView.setText(String.valueOf(position));
转换position
为字符串。