4

I am trying to make a sample app using the FirebaseRecyclerAdapter but keep getting the error java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View] have tried everything but nothing seems to work

the error:

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)

my Main Activity, MainActivity.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);
        }
    }
}

the Data object, ailment.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;
    }
}

The two layout files, This one is for the Main Activity, 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>

This is for the row of the recycler view, 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>

Edit: The Dependencies

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'
}
4
  • what are your gradle dependencies, also do you by chance use Multidex Commented Jan 29, 2017 at 23:12
  • @EpicPandaForce Haven't used MultiDex. btw I have added the dependencies Commented Jan 29, 2017 at 23:19
  • hmm what is viewHolder=new DataHolder(getWindow().getDecorView().getRootView(),getApplicationContext()); and why? Commented Jan 29, 2017 at 23:30
  • Tried to initialize DataHolder constructor when nothing else seemed to work. Commented Jan 30, 2017 at 9:00

1 Answer 1

11

Your DataHoldershould be public like this

public static class DataHolder extends RecyclerView.ViewHolder{...}
Sign up to request clarification or add additional context in comments.

2 Comments

também quero saber.
with static it's working fine. There is problem with firebase ui document github.com/firebase/FirebaseUI-Android/tree/master/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.