0

My activity has a recyclerview , my code has no errors in the IDE and accepts all support libraries, but when I try to run my code the app crashes.

what is the problem ?

package com.ed.test4;



import java.util.ArrayList;
import java.util.List; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private RecyclerView contactsRecyclerView;

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

    contactsRecyclerView = (RecyclerView) findViewById(R.id.contacts_recyclerview);
    contactsRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    List<Contact> contacts = new ArrayList<>();
    for (int i = 0; i < 50; i++) {
        contacts.add(new Contact("Contact #" + i));
    }

    ContactsAdapter adapter = new ContactsAdapter(contacts);
    contactsRecyclerView.setAdapter(adapter);
}

private class ContactsAdapter extends RecyclerView.Adapter<ContactHolder> {
    private List<Contact> contacts;

    public ContactsAdapter(List<Contact> contacts) {
        this.contacts = contacts;
    }

    @Override
    public ContactHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
        View v = inflater.inflate(R.layout.list_item, parent, false);
        return new ContactHolder(v);
    }

    @Override
    public void onBindViewHolder(ContactHolder holder, int position) {
        Contact contact = contacts.get(position);
        holder.tvContactName.setText(contact.getName());
        holder.tvContactRegisterDate.setText(contact.getRegisterDate().toString());
    }

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

private class ContactHolder extends RecyclerView.ViewHolder {
    private TextView tvContactName, tvContactRegisterDate;

    public ContactHolder(View itemView) {
        super(itemView);
        tvContactName = (TextView) itemView.findViewById(R.id.contact_name_textview);
        tvContactRegisterDate = (TextView) itemView.findViewById(R.id.contact_register_date_textview);
    }
}


public class Contact {
private String name;
private Date registerDate;

public Contact(String name) {
    this.name = name;
    registerDate = new Date();
    }

public String getName() {
    return name;
    }

public Date getRegisterDate() {
    return registerDate;
    }

public void setName(String name) {
    this.name = name;
    }
}
    }

and this is screenshot of logcat shows error

Farhad
  • 12,178
  • 5
  • 32
  • 60
roz
  • 27
  • 1
  • 8
  • 1
    Please share Manifest & Xml file – Ajinkya Feb 17 '16 at 10:16
  • Refer this link. It may help you http://stackoverflow.com/questions/31672233/recyclerview-class-android-support-v7-recyclerview-rstyleable-can-not-be-found and this too http://stackoverflow.com/questions/20900832/java-lang-noclassdeffounderror-android-support-v7-appcompat-rstyleable – ajantha Feb 17 '16 at 10:40

1 Answers1

0

I am assuming that this issue occurs in eclipse only as in android studio you just need to add the compile version something like

compile 'com.android.support:recyclerview-v7:22.0.0'

in dependencies section.

In eclipse to make RecyclerView work you need to add recycler view library in your project. As you are saying that it is not showing any error it means you have added recycler view as library project and you are just one step behind to make it work.

Note:- You haven't added recycler view library project into your application then go to Android/Sdk/extras/android/support/v7/recyclerview path and import this project.

Now Right click on project -> go to java build path -> select project tab ->click on add -> choose recycler view project

Now clean the build and run. It will work.

  • tnx but my issue now is this error in xml file The following classes could not be instantiated: - android.support.v7.widget.RecyclerView (Open Class, Show Error Log) – roz Feb 19 '16 at 19:36
  • add recycler view library through properties too and also check if you are having jar dependencies for the same. – Praween Kumar Mishra Feb 22 '16 at 13:56