这是我在Android Studio中的第一个应用程序,我无法解决此错误,我需要帮助,谢谢。
E/RecyclerView: No adapter attached; skipping layout
E/RecyclerView: No adapter attached; skipping layout
MainActivity
public class MainActivity extends AppCompatActivity {
private ViewPager mPager;
private SlidingTabLayout mTabs;
private Toolbar toolbar;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//toolbar
toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Sliding Tab
mPager = (ViewPager) findViewById(R.id.pager);
mTabs= (SlidingTabLayout) findViewById(R.id.tabs);
mPager.setAdapter(new MyAdapter(getSupportFragmentManager(),this));
mTabs.setDistributeEvenly(true);
mTabs.setCustomTabView(R.layout.tab_view, R.id.tabTeks);
mTabs.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.colorAccent));
mTabs.setViewPager(mPager);
}
我的适配器
public class MyAdapter extends FragmentPagerAdapter {
private Context mContext;
private String[] titles ={"OVERVIEW","PORTOFOLIO","TEAM"};
int [] icons = new int []{R.mipmap.ic_adopcion, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
private int heightIcon;
public MyAdapter(FragmentManager fm, Context c) {
super(fm);
mContext=c;
double scale=c.getResources().getDisplayMetrics().density;
heightIcon=(int)(30*scale+0.5f);
}
@Override
public Fragment getItem(int position) {
Fragment frag=null;
if (position==0){
frag = new OverviewFragment();
} else if(position==1){
frag= new TeamFragment();
} else if(position==2){
frag = new PortofolioFragment();
}
Bundle b = new Bundle();
b.putInt("position", position);
frag.setArguments(b);
return frag;
}
@Override
public int getCount() {
return titles.length;
}
@Override
public CharSequence getPageTitle(int position) {
Drawable d = mContext.getResources().getDrawable(icons[position]);
d.setBounds(0,0,heightIcon,heightIcon);
ImageSpan is = new ImageSpan(d);
SpannableString sp = new SpannableString(" ");
sp.setSpan(is,0,sp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return (sp);
}
ItemAdapter
public class ItemAdapter extends RecyclerView.Adapter<ItemHolder> {
private List<Perros> PerrosList;
public ItemAdapter(List<Perros> PerrosList){
this.PerrosList = PerrosList;
}
@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_layout, parent, false);
return new ItemHolder(itemView);
}
@Override
public void onBindViewHolder(ItemHolder holder, int position) {
Perros item = PerrosList.get(position);
holder.txtCelebName.setText(item.getName());
holder.txtCelebMovie.setText(item.getFamousMovie());
Picasso.with(holder.txtCelebName.getContext()).load(item.getProfilePhotoLocation()).into(holder.profileImage);
Picasso.with(holder.txtCelebName.getContext()).load(item.getImgsex()).into(holder.imgsex);
}
@Override
public int getItemCount() {
return PerrosList.size();
}
片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_overview, container, false);
//return inflater.inflate(R.layout.fragment_overview, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRequestQueue = MySingleton.getInstance().getRequestQueue();
array = new JsonObjectRequest(Request.Method.GET, url, "", new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
itemAdapter = new ItemAdapter(getPerros(response));
recyclerView.setAdapter(itemAdapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG,error.toString());
}
});
mRequestQueue.add(array);
return rootView;
//return inflater.inflate(R.layout.fragment_overview, container, false);
}
private List<Perros> getPerros(JSONObject jsonObject){
List<Perros> lista = new ArrayList<>();
try {
JSONArray array= jsonObject.getJSONArray("alumnos");
for (int i=0; i<array.length(); i++){
JSONObject objeto = array.getJSONObject(i);
Perros perro = new Perros();
perro.setName(objeto.getString("nombre"));
perro.setFamousMovie(objeto.getString("direccion"));
perro.setProfilePhotoLocation(img+objeto.getString("rutaimagen"));
perro.setImgsex(img+objeto.getString("sexo"));
lista.add(perro);
}
} catch (JSONException e) {
e.printStackTrace();
}
return lista;
}
答案 0 :(得分:0)
在onCreate()方法中获取ArrayAdapter中的数据之前,需要设置空白适配器。第一 所有在getItemCount()方法的ItemAdapter中放在下面的行。
return (null != PerrosList ? PerrosList.size() : 0);
答案 1 :(得分:0)
将onCreateView
代码更改为此
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_overview, container, false);
//return inflater.inflate(R.layout.fragment_overview, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRequestQueue = MySingleton.getInstance().getRequestQueue();
itemAdapter = new ItemAdapter(new ArrayList<Perros>());
array = new JsonObjectRequest(Request.Method.GET, url, "", new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
itemAdapter.setData(getPerros(response));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG,error.toString());
}
});
mRequestQueue.add(array);
return rootView;
//return inflater.inflate(R.layout.fragment_overview, container, false);
}
在适配器代码中添加此方法
void setData(List<Perros> data)
{
this.PerrosList.clear();
for(Perros p:data){
this.PerrosList.add(p);
}
notifyDataSetChanged();
}
答案 2 :(得分:0)
在创建阶段未设置适配器时会发生这种情况:在api响应获取时调用notifyDataSetChanged()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
magazineAdapter = new MagazineAdapter(getContext(), null, this );
newClipRecyclerView.setAdapter(magazineAdapter);
magazineAdapter.notifyDataSetChanged();
APICall();
}
public void APICall() {
if(Response.isSuccessfull()){
mRecyclerView.setAdapter(mAdapter);
}
}
Just move setting the adapter into onCreate with an empty data and when you have the data call:
mAdapter.notifyDataSetChanged();