问题
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)
回答1:
I was simply missing :
public ChatMessage(){}
on my ChatMessage.class
- thanks @Michael for finding me the solution.
回答2:
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
来源:https://stackoverflow.com/questions/52162502/class-does-not-define-a-no-argument-constructor-if-you-are-using-proguard-make