2016-02-17 60 views
0

我的活動有一個recyclerview,我的代碼在IDE中沒有錯誤並接受所有支持庫,但是當我嘗試運行我的代碼時,應用程序崩潰。Android中的RecyclerView的NoClassDefFound

是什麼問題?

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

+1

請分享Manifest&XML文件 – Ajinkya

+0

請參閱此鏈接。它可以幫助你http://stackoverflow.com/questions/31672233/recyclerview-class-android-support-v7-recyclerview-rstyleable-can-not-be-found,這也太http://stackoverflow.com/questions/ 20900832/java-lang-noclassdeffounderror-android-support-v7-appcompat-rstyleable – ajantha

回答

0

我假設,在日食只在Android Studio中會出現這個問題,你只需要在依賴關係部分添加編譯版本類似

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

在eclipse中使RecyclerView工作,你需要在你的項目中添加回收視圖庫。正如你所說的那樣,它沒有顯示任何錯誤,這意味着你已經將回收站視圖添加爲圖書館項目,並且你只是落後一步才能使之工作。

注意: - 您尚未將回收站視圖庫項目添加到您的應用程序中,然後轉至 Android/Sdk/extras/android/support/v7/recyclerview路徑並導入此項目。

項目現在右鍵 - >轉到Java構建路徑 - >選擇項目選項卡 - >單擊添加 - >選擇回收視圖項目

現在清理構建和運行。它會工作。

+0

tnx但我現在的問題是xml文件中的這個錯誤 以下類無法實例化: - android.support.v7.widget.RecyclerView (打開類,顯示錯誤日誌) – roz

+0

也通過屬性添加回收視圖庫,並檢查您是否具有相同的jar依賴項。 –