20

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)
olajide
  • 972
  • 1
  • 13
  • 26
Cagri Karabulut
  • 313
  • 1
  • 3
  • 14
  • _" i am getting Class does not define a no-argument constructor"_. _Which_ class? Please post the full stacktrace. – Michael Sep 04 '18 at 09:03
  • @Michael 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. – Cagri Karabulut Sep 04 '18 at 09:24
  • 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) – Cagri Karabulut Sep 04 '18 at 09:25
  • 1
    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? – Michael Sep 04 '18 at 09:30
  • Hi @Michael, i added ChatMessage on my first post, thanks – Cagri Karabulut Sep 04 '18 at 09:35
  • 1
    Well, as the error message says, your `ChatMessage` class does not have a no-argument constructor. So you need to add one. – Michael Sep 04 '18 at 09:59
  • oh yes! easy solution, thanks a lot! – Cagri Karabulut Sep 04 '18 at 10:06

13 Answers13

50

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)
Abili Isaac
  • 574
  • 4
  • 5
13

I was simply missing :

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

olajide
  • 972
  • 1
  • 13
  • 26
Cagri Karabulut
  • 313
  • 1
  • 3
  • 14
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**
Urvish Jani
  • 104
  • 1
  • 4
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

dreinoso
  • 1,479
  • 17
  • 26
5

Just add this constructor

public ChatMessage(){}
4

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

public ChatMessage() { }
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
Ubaldito
  • 41
  • 3
3

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

public ChatMessage() { }
3

You'll have to add a default constructor:

public ChatMessage() { }
aboger
  • 2,214
  • 6
  • 33
  • 47
3

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)

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,
)
Night Coder
  • 45
  • 2
  • 6
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,
Rishita Joshi
  • 203
  • 2
  • 3
0

Use like that

  data class FirebaseModelClass(
        var Name:String?="",
        var Rep:String?="",
        var Text:String?="",
        var Time:String?="",
        var Url:String?=""
    )
Sam
  • 241
  • 3
  • 7
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(){}

Moeenuddin178
  • 39
  • 1
  • 9