我正在尝试使用 FirebaseRecyclerAdapter 制作一个示例应用程序,但不断收到错误java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View]
尝试了所有方法,但似乎没有任何效果
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.visan.newappfireui, PID: 26711
java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View]
at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:172)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6078)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5248)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5158)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1445)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3379)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3188)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1595)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:323)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:919)
at android.view.Choreographer.doCallbacks(Choreographer.java:710)
at android.view.Choreographer.doFrame(Choreographer.java:642)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:905)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5582)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NoSuchMethodException: <init> [class android.view.View]
at java.lang.Class.getConstructor(Class.java:528)
at java.lang.Class.getConstructor(Class.java:492)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:169)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6078)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5248)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5158)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1445)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3379)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3188)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1595)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:323)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:919)
at android.view.Choreographer.doCallbacks(Choreographer.java:710)
at android.view.Choreographer.doFrame(Choreographer.java:642)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:905)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5582)
at java.lang.reflect.Method.invoke(Native Method)
我的主要活动,主要活动.java
package com.example.visan.newappfireui;
import android.content.Context;
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.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
private FirebaseRecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseReference reference= FirebaseDatabase.getInstance().getReference().child("ailment");
RecyclerView recyclerView=(RecyclerView)findViewById(R.id.recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter =new FirebaseRecyclerAdapter<ailment,DataHolder>(ailment.class,R.layout.recycler_row,DataHolder.class,reference){
@Override
protected void populateViewHolder(DataHolder viewHolder, ailment model, int position) {
viewHolder=new DataHolder(getWindow().getDecorView().getRootView(),getApplicationContext());
viewHolder.setName(model.getName());
viewHolder.setImg(model.getImg());
}
};
recyclerView.setAdapter(adapter);
}
@Override
protected void onDestroy() {
super.onDestroy();
adapter.cleanup();
}
static class DataHolder extends RecyclerView.ViewHolder{
public ImageView imageView;
public TextView textView;
private final Context context;
public DataHolder(View v, Context context){
super(v);
imageView=(ImageView)v.findViewById(R.id.imageView);
textView=(TextView)v.findViewById(R.id.textView);
this.context=context;
}
public void setName(String name){
textView.setText(name);
}
public void setImg(String img){
Picasso.with(context).load(img).centerCrop().into(imageView);
}
}
}
数据对象,疾病.java
package com.example.visan.newappfireui;
public class ailment {
private String name;
private String img;
public ailment(){
}
public ailment(String name,String img){
this.name=name;
this.img=img;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getImg(){
return img;
}
public void setImg(String img){
this.img=img;
}
}
两个布局文件,这是主要活动的,activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.visan.newappfireui.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler"/>
</RelativeLayout>
这是针对回收器视图的行,recycler_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@drawable/common_full_open_on_phone"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/imageView" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_alignParentStart="true"
android:id="@+id/textView" />
</RelativeLayout>
编辑:依赖
关系dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.google.firebase:firebase-storage:10.0.1'
compile 'com.google.firebase:firebase-auth:10.0.1'
compile 'com.google.firebase:firebase-database:10.0.1'
compile 'com.firebaseui:firebase-ui-database:1.0.1'
compile 'com.firebaseui:firebase-ui-storage:1.0.1'
compile 'com.android.support:support-v4:23.4.0'
compile 'com.squareup.picasso:picasso:2.5.2'
testCompile 'junit:junit:4.12'
}
你的DataHolder
应该是这样的公开的
public static class DataHolder extends RecyclerView.ViewHolder{...}