Class does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped
Asked Answered
A

13

21

I am trying to create a chat app on android studio. my app has no build errors and i can start the app. However, when i use listOfMessage.setAdapter(adapter); my app is crashing. it does start the app though, after i would say 1-2 secs it is crashing the app. on logcat, i am getting Class does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped error message. can you please advise me how to resolve this?

When i do not use listOfMessage.setAdapter(adapter);, whatever i write down on the app doesn't show on the listview, but i see entries are going through fine to firebase. code below:

public class MainActivity extends AppCompatActivity {



private static int SIGN_IN_REQUEST_CODE = 1;
private FirebaseListAdapter<ChatMessage> adapter;
RelativeLayout activity_main;
FloatingActionButton sendButton;


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

    activity_main =(RelativeLayout)findViewById(R.id.activiy_main);
    sendButton = (FloatingActionButton) findViewById(R.id.sendButton);
    sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            EditText input = (EditText)findViewById(R.id.input);
            FirebaseDatabase.getInstance().getReference().push().setValue(new ChatMessage(input.getText().toString(),
                    FirebaseAuth.getInstance().getCurrentUser().getEmail()));
            input.setText("");
        }
    });


    if(FirebaseAuth.getInstance().getCurrentUser() == null) {
        startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().build(), SIGN_IN_REQUEST_CODE);
    }
    else
    {
        Snackbar.make(activity_main,"Welcome "+FirebaseAuth.getInstance().getCurrentUser().getEmail(),Snackbar.LENGTH_SHORT).show();
        displayChatMessage();
    }





}
private void displayChatMessage() {
    ListView listOfMessage = (ListView)findViewById(R.id.list_of_message);
    adapter = new FirebaseListAdapter<ChatMessage>(this,ChatMessage.class,R.layout.list_item,FirebaseDatabase.getInstance().getReference())
    {
        @Override
        protected void populateView(View v, ChatMessage model, int position) {
            TextView messageText, messageUser,messageTime;
            messageText = (TextView)v.findViewById(R.id.message_text);
            messageUser = (TextView)v.findViewById(R.id.message_user);
            messageTime = (TextView)v.findViewById(R.id.message_time);

            messageText.setText(model.getMessageText());
            messageUser.setText(model.getMessageText());
            messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",model.getMessageTime()));
        }
    };


    //BELOW ONE NEEDS TO BE ACTIVE...TO MAKE ENTRIES VISIBLE

    //listOfMessage.setAdapter(adapter);



}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SIGN_IN_REQUEST_CODE)
    {
        if(resultCode == RESULT_OK)
        {
            Snackbar.make(activity_main, "Successfully signed in. Welcome !", Snackbar.LENGTH_SHORT).show();
            displayChatMessage();
        }
        else {
            Snackbar.make(activity_main, "we couldn't sign you in. Please try again", Snackbar.LENGTH_SHORT).show();
            finish();
        }
    }
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == R.id.menu_sign_out)
    {
        AuthUI.getInstance().signOut(this).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                Snackbar.make(activity_main,"You have been signed out..", Snackbar.LENGTH_SHORT).show();
                finish();
            }
        });
    }
    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu,menu);
    return true;
}

}

and ChatMessage.class below :

public class ChatMessage {

    private String messageText;
    private String messageUser;
    private  long messageTime;

    public ChatMessage(String messageText, String messageUser) {
        this.messageText = messageText;
        this.messageUser = messageUser;

        messageTime = new Date().getTime();
    }

    public ChatMessage(String messageText) {
        this.messageText = messageText;
    }

    public String getMessageText() {
        return messageText;
    }

    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }

    public String getMessageUser() {
        return messageUser;
    }

    public void setMessageUser(String messageUser) {
        this.messageUser = messageUser;
    }

    public long getMessageTime() {
        return messageTime;
    }

    public void setMessageTime(long messageTime) {
        this.messageTime = messageTime;
    }
}

Full error on logcat :

com.google.firebase.database.DatabaseException: Class com.example.cagrik.chat.ChatMessage does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
        at com.google.android.gms.internal.firebase_database.zzku.zza(Unknown Source:44)
        at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source:196)
        at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source:0)
        at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
        at com.firebase.ui.database.FirebaseListAdapter.parseSnapshot(FirebaseListAdapter.java:120)
        at com.firebase.ui.database.FirebaseListAdapter.getItem(FirebaseListAdapter.java:109)
        at com.firebase.ui.database.FirebaseListAdapter.getView(FirebaseListAdapter.java:137)
        at android.widget.AbsListView.obtainView(AbsListView.java:3189)
        at android.widget.ListView.makeAndAddView(ListView.java:2197)
        at android.widget.ListView.fillDown(ListView.java:824)
        at android.widget.ListView.fillFromTop(ListView.java:885)
        at android.widget.ListView.layoutChildren(ListView.java:1924)
        at android.widget.AbsListView.onLayout(AbsListView.java:2961)
        at android.view.View.layout(View.java:20854)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
        at android.view.View.layout(View.java:20854)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20854)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
        at android.view.View.layout(View.java:20854)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20854)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:20854)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:945)
        at android.view.View.layout(View.java:20854)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2956)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2643)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1780)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7827)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Ansel answered 4/9, 2018 at 8:56 Comment(7)
" i am getting Class does not define a no-argument constructor". Which class? Please post the full stacktrace.Servility
@Servility com.google.firebase.database.DatabaseException: Class com.example.cagrik.chat.ChatMessage does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.Ansel
at com.google.android.gms.internal.firebase_database.zzku.zza(Unknown Source:44) at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source:196) at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source:0) at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10) at com.firebase.ui.database.FirebaseListAdapter.parseSnapshot(FirebaseListAdapter.java:120) at com.firebase.ui.database.FirebaseListAdapter.getItem(FirebaseListAdapter.java:109) at com.firebase.ui.database.FirebaseListAdapter.getView(FirebaseListAdapter.java:137)Ansel
Post it properly formatted in your question by editing it. Also, where is the code for the ChatMessage class, since that's what the error refers to?Servility
Hi @Michael, i added ChatMessage on my first post, thanksAnsel
Well, as the error message says, your ChatMessage class does not have a no-argument constructor. So you need to add one.Servility
oh yes! easy solution, thanks a lot!Ansel
V
56

This is late but probably will help someone in future Kotlin

**change your from**

data class ChatMessage(
   var messageText: String?,
   var messageUser: String?,
   var messageTime: Long?)

to this

data class ChatMessage(
   var messageText: String? = "",
   var messageUser: String? = "",
   var messageTime: Long? = 0)
Verner answered 28/4, 2021 at 10:2 Comment(1)
I can't stress enough how much I appreciated this. Thank you so much!Magically
A
14

I was simply missing :

public ChatMessage(){} on my ChatMessage.class - thanks @Michael for finding me the solution.

Ansel answered 4/9, 2018 at 14:13 Comment(1)
i don't understand your solution because in your question code you had already the ChatMessage constructor???Lysander
D
7

Solution for Kotlin

You need to put secondary constructor

Add following code in ChatMessage.kt

 constructor(
    messageText: String,
    messageUser: String,
    messageUserId: String) {
    this.messageText = messageText
    this.messageUser = messageUser
    messageTime = Date().getTime()
    this.messageUserId = messageUserId
}

constructor()   // **Add this**
Downcast answered 27/7, 2020 at 9:22 Comment(1)
What if I'm doing this in data class?Cachet
N
6

Just add this constructor

public ChatMessage(){}
Northway answered 27/2, 2021 at 4:31 Comment(0)
P
5

Also you could get that message if you are using an inner class. Then the best would be to extract that inner class to it's own file, in this case: ChatMessage.java

Piddle answered 26/7, 2019 at 18:17 Comment(0)
C
4

I've the same Error , ive forgotten the constructor :

public ChatMessage() { }
Crysta answered 23/8, 2020 at 6:37 Comment(0)
P
4

Add a default parameter to the data class in your kotlin code and set each of them to null. E.g.

data class Message(val name: String? = null, val userName: String? = null)

Proterozoic answered 7/2, 2021 at 4:9 Comment(0)
N
3

Just add an empty constructor in your bean file after declaring all the variables i.e

public ChatMessage() { }
Natala answered 20/6, 2020 at 10:29 Comment(0)
E
3

You'll have to add a default constructor:

public ChatMessage() { }
Eudiometer answered 13/10, 2020 at 12:7 Comment(0)
E
1

Just initialise your variable with default values like

data class User(
   val name: String? = null,
   val desc: String? = null,
   val image: String? = null,
   val userId: String? = null,
   val msgToken: String? = null,
)
Empyrean answered 22/6, 2023 at 9:2 Comment(0)
P
0

To solve this problem you have to give right argument type to the declared variable in the model class.

val audio: List<Audio>?=null,
    val image: String ?=null,
    val latitude: Long ?= null,
    val locationName: String ?=null,
    val longitude: Long ?= null,
Prisilla answered 9/5, 2022 at 9:6 Comment(0)
H
0

Use like that

  data class FirebaseModelClass(
        var Name:String?="",
        var Rep:String?="",
        var Text:String?="",
        var Time:String?="",
        var Url:String?=""
    )
Heliogravure answered 1/11, 2022 at 7:23 Comment(0)
P
0

If we Read the Line Class does not define a no-argument constructor.

We clearly see compiler tells us empty constructor mean no-argument is not present

public ChatMessage(){}

Passover answered 6/2, 2023 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.