簡體   English   中英

Android:E / RecyclerView:未連接適配器; 跳過布局

[英]Android: E/RecyclerView: No adapter attached; skipping layout

所以我有一個選項卡式活動,其中包含3個選項卡作為片段,每個選項卡都有一個RecyclerView。

我在這里和其他站點上檢查了所有已回答的問題,一切似乎都很好,但是它不起作用!

這是我的代碼:

MainActivity.java:

package esprit.tn.mywaterproject;

import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;
import android.widget.Toast;

import java.time.Duration;
import java.util.zip.Inflater;

public class MainActivity extends AppCompatActivity {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link FragmentPagerAdapter} derivative, which will keep every
 * loaded fragment in memory. If this becomes too memory intensive, it
 * may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
 private SectionsPagerAdapter mSectionsPagerAdapter;

 /**
 * The {@link ViewPager} that will host the section contents.
 */
 private ViewPager mViewPager;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.e("test","test log");
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new             
    SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new     
    TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new 
    TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

    FloatingActionButton fab =  findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "Chat avec un 
    résponsable", Toast.LENGTH_SHORT).show();

        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.e("frag number", "frag test number 
:"+getArguments().getInt(ARG_SECTION_NUMBER));
        if (getArguments().getInt(ARG_SECTION_NUMBER)==1){
            Log.e("frag test1", "frag test TEST1");
            new Eaux_Fragment();
            return inflater.inflate(R.layout.fragment_eaux, 
container,false);

        }
        if (getArguments().getInt(ARG_SECTION_NUMBER)==2){
            Log.e("frag test2", "frag test TEST2");
            return inflater.inflate(R.layout.fragment_piscine, 
container,false);
        }
        if (getArguments().getInt(ARG_SECTION_NUMBER)==3){
            Log.e("frag test3", "frag test TEST3");
            return inflater.inflate(R.layout.fragment_electricite, container,false);
        }

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }
}
}

這是我的Adapter ProduitAdapter.java:

package esprit.tn.mywaterproject;

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;

import java.util.ArrayList;
import java.util.List;

import esprit.tn.mywaterproject.Entities.Produit_Eau;

public class ProduitAdapter extends 
RecyclerView.Adapter<ProduitAdapter.ViewHolder> {

private Context context;
private List<Produit_Eau> list;

public ProduitAdapter( Context context, ArrayList<Produit_Eau> list) {
    this.context=context;
    this.list = list;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Log.e("LOG IN ADAPTER","TEST ADAPTER");
    View v =     
 LayoutInflater.from(context).inflate(R.layout.single_produit_eau, parent, 
false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Produit_Eau prod_eau = list.get(position);


    holder.text_single_prod_nom.setText(prod_eau.getNom());
    holder.text_single_prod_description.setText(prod_eau.getDescription());

}

@Override
public int getItemCount() {
    return list.size();
 }

 public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView text_single_prod_nom, text_single_prod_description;

    public ViewHolder(View itemView) {
        super(itemView);

        text_single_prod_nom = itemView.findViewById(R.id.single_prod_nom);
        text_single_prod_description = 
itemView.findViewById(R.id.single_prod_description);
    }
}

}

這是片段Eau_Fragment.java之一

package esprit.tn.mywaterproject.Fragments;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
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.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import esprit.tn.mywaterproject.Entities.Produit_Eau;
import esprit.tn.mywaterproject.ProduitAdapter;
import esprit.tn.mywaterproject.R;


/**
* A simple {@link Fragment} subclass.
*/
public class Eaux_Fragment extends Fragment {
private RecyclerView recyclerList;

private LinearLayoutManager linearLayoutManager;

private ArrayList<Produit_Eau> produit_eauList;

private ProduitAdapter adapter;
private String UrlShowProducts = "http://192.168.1.7:3003/prodeau";
private LinearLayout linearLayout;

public Eaux_Fragment() {
    Log.e("Test Eau Fragment","EAU FRAG TEST");

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragmentd
    View view = inflater.inflate(R.layout.fragment_eaux, container, false);
    linearLayoutManager = new 
LinearLayoutManager(getActivity().getApplicationContext());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

DividerItemDecoration(recyclerList.getContext(), 
linearLayoutManager.getOrientation());
    adapter = new ProduitAdapter(getActivity().getApplicationContext(), 
produit_eauList);

    recyclerList = getActivity().findViewById(R.id.eau_prod_list);
    recyclerList.setHasFixedSize(true);
    recyclerList.setLayoutManager(linearLayoutManager);

    produit_eauList = new ArrayList<>();
    getData();
    recyclerList.setAdapter(adapter);

    adapter.notifyDataSetChanged();

    return view;
}

private void getData() {


    JsonArrayRequest jsonArrayRequest = new 
JsonArrayRequest(UrlShowProducts, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            for (int i = 0; i < response.length(); i++) {
                try {
                    JSONObject jsonObject = response.getJSONObject(i);

                    Produit_Eau produit_eau = new Produit_Eau();
                    produit_eau.setNom(jsonObject.getString("name"));

 produit_eau.setDescription(jsonObject.getString("description"));

                    produit_eauList.add(produit_eau);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            adapter.notifyDataSetChanged();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Volley", error.toString());
        }
    });
    RequestQueue requestQueue = 
Volley.newRequestQueue(getActivity().getApplicationContext());
    requestQueue.add(jsonArrayRequest);

}
}

編輯:我在這里面臨的問題是Eau_Fragment沒有顯示在應用程序中。 我刪除了所有其他片段,並僅保留該片段對其進行測試,該片段顯示為空,而我得到的唯一錯誤是上述錯誤。

這只是一個警告,但如果您對此有所擔心,請在布局管理器之前設置適配器:

recyclerList = getActivity().findViewById(R.id.eau_prod_list);
recyclerList.setAdapter(adapter);
recyclerList.setHasFixedSize(true);
recyclerList.setLayoutManager(linearLayoutManager);

因為它會在您設置布局管理器時立即觸發布局更新,因此最終會尋找適配器。

我不確定您面臨的問題是什么,但是我注意到的一件事是您沒有在SectionsPagerAdapter中使用Eaux_Fragment,因此需要進行更改:

    return PlaceholderFragment.newInstance(position + 1);

   return Eaux_Fragment(position + 1);

並且不要忘記將newInstance方法添加到Eaux_Fragment

public static Eaux_Fragment newInstance(int position) {
        Eaux_Fragment fragment = new Eaux_Fragment();
        Bundle args = new Bundle();
        args.putString(ARG_POSITION, position);
        fragment.setArguments(args);
        return fragment;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM