Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

java - No adapter attached; skipping layout recyclerview error

No adapter attached; skipping layout

i m fetching json data from url to recyclerview i have toasted response i fetched response correctly but not able to set.

javaclasses are below

    package "";

public class MainRecyclerView extends ActionBarActivity {
    RecyclerView recyclerView;
    LinearLayout linearLayout;
    RecyclerViewListview adapter;
    List<Data> dataList = new ArrayList<Data>();
    public String URL = "http://toscanyacademy.com/blog/mp.php";
    private RecyclerView.LayoutManager layoutManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_recycler);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RecyclerViewListview(this, dataList);

        recyclerView.setAdapter(adapter);
        RequestJsonArray();

    }

    public void getData(JSONArray jsonArray) {
        for (int i = 0; i < jsonArray.length(); i++) {
            Data data = new Data();
            JSONObject jsonObject = null;
            try {
                jsonObject = jsonArray.getJSONObject(i);
                data.setSong_name(jsonObject.getString("song_name"));
                data.setSong_year(jsonObject.getInt("song_id"));
                data.setSong_author(jsonObject.getString("artist_name"));
                recyclerView.setAdapter(adapter);

            } catch (JSONException e) {
                e.printStackTrace();
            }
            dataList.add(data);
            Toast.makeText(MainRecyclerView.this, dataList.toString(), Toast.LENGTH_SHORT).show();


        }


         adapter.notifyDataSetChanged();


    }

    public void RequestJsonArray() {

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, URL, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                // String songname = response.getString();
                getData(response);
                Toast.makeText(MainRecyclerView.this, response.toString(), Toast.LENGTH_SHORT).show();


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainRecyclerView.this, error.toString(), Toast.LENGTH_SHORT).show();

            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsonArrayRequest);
    }

}

second class

package"";

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

    private Context context;
    private LayoutInflater layoutInflater;
    List<Data> dataList;

    public RecyclerViewListview(Context context, List<Data> dataList) {
        this.context = context;
        this.dataList = dataList;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

       // View view = layoutInflater.inflate(R.layout.recycle_list_raw, parent, false);
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_list_raw, null);
        MyHolder holder = new MyHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder parent, int position) {

        MyHolder myHolder = (MyHolder) parent;
        myHolder.song_name.setText(dataList.get(position).getSong_name());
        myHolder.song_year.setText(dataList.get(position).getSong_year());
        myHolder.song_year.setText(dataList.get(position).getSong_author());
    }

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

    class MyHolder extends RecyclerView.ViewHolder {

        public TextView song_name;
        public TextView song_year;
        public TextView song_author;

        // create constructor to get widget reference
        public MyHolder(View itemView) {
            super(itemView);
            song_name = (TextView) itemView.findViewById(R.id.stitle);
            song_year = (TextView) itemView.findViewById(R.id.s_year);
            song_author = (TextView) itemView.findViewById(R.id.sauthor);

        }

    }
}

third class public class Data {

    public String song_name;
    public int song_year;
    public String song_author;

    public Data() {
    }

    /*public Data(String song_name, String song_year, String song_author) {
        this.song_name = song_name;
        this.song_year = song_year;
        this.song_author = song_author;
    }*/


    public String getSong_author() {
        return song_author;
    }

    public void setSong_author(String song_author) {
        this.song_author = song_author;
    }

    public String getSong_name() {
        return song_name;
    }

    public void setSong_name(String song_name) {
        this.song_name = song_name;
    }

    public int getSong_year() {
        return song_year;
    }

    public void setSong_year(int song_year) {
        this.song_year = song_year;
    }

}

Logcate

 08-12 19:16:59.262 29356-29370/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.Restarter.getActivities
08-12 19:24:18.958 2054-2054/com.example.altaf.shopper E/InstantRun: Could not find slices in APK; aborting.
08-12 19:24:18.962 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
08-12 19:24:18.962 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.970 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.transition.TransitionManager', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.974 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.982 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.986 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:18.986 2054-2054/com.example.altaf.shopper E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.altaf.shopper.volley.MainRecyclerView.access$super
08-12 19:24:19.982 2054-2054/com.example.altaf.shopper E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         android.content.res.Resources$NotFoundException: String resource ID #0x7c6
                                                                             at android.content.res.Resources.getText(Resources.java:230)
                                                                             at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
                                                                             at android.widget.TextView.setText(TextView.java:3769)
                                                                             at com.example.altaf.shopper.volley.RecyclerViewListview.onBindViewHolder(RecyclerViewListview.java:46)
                                                                             at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
                                                                             at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
                                                                             at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
                                                                             at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
                                                                             at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
                                                                             at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
                                                                             at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
                                                                             at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
                                                                             at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
                                                                             at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906)
                                                                             at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
                                                                             at android.view.View.layout(View.java:14008)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:4373)
                                 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

there is a blunder in MainRecyclerView's onCreate()

 recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

 dataList = new ArrayList<>();     // your dataList is empty here...
 recyclerView.setAdapter(adapter); // your Adapter is null here. (not initialized)
 RequestJsonArray();

You need to call method which prepares your dataList before passing it to Adapter. Do call methods at proper position.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_recycler);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);        
    recyclerView.setHasFixedSize(true);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

    dataList = new ArrayList<>();

    RequestJsonArray();

    adapter = new RecyclerViewListview(this, dataList);        
    recyclerView.setAdapter(adapter);
}

Now inside your getData() comment out lines after dataList.add(data);, then clean project, rebuild then run.

Now as you have edited your question: try like this in onBindViewHolder() of RecyclerAdapter.

myHolder.song_name.setText("" + dataList.get(position).getSong_name());
myHolder.song_year.setText("" + dataList.get(position).getSong_year());
myHolder.song_year.setText("" + dataList.get(position).getSong_author());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...