Приложение Android Messenger падает после входа в систему - PullRequest
0 голосов
/ 06 мая 2018

Это отображается в logcat; как это исправить?

com.google.firebase.database.DatabaseException: Class com.chatapp.ChatMessage не определяет конструктор без аргументов. Если вы используете ProGuard, убедитесь, что эти конструкторы не удалены. на com.google.android.gms.internal.firebase_database.zzku.zza (Неизвестно Источник) на com.google.android.gms.internal.firebase_database.zzkt.zzb (неизвестно Источник) на com.google.android.gms.internal.firebase_database.zzkt.zza (Неизвестно Источник) на com.google.firebase.database.DataSnapshot.getValue (неизвестный источник) в com.firebase.ui.database.FirebaseListAdapter.parseSnapshot (FirebaseListAdapter.java:127) в com.firebase.ui.database.FirebaseListAdapter.getItem (FirebaseListAdapter.java:116) в com.firebase.ui.database.FirebaseListAdapter.getView (FirebaseListAdapter.java:146) на android.widget.AbsListView.obtainView (AbsListView.java:2370) на android.widget.ListView.makeAndAddView (ListView.java:1970) на android.widget.ListView.fillDown (ListView.java:704) на android.widget.ListView.fillFromTop (ListView.java:765) на android.widget.ListView.layoutChildren (ListView.java:1716) на android.widget.AbsListView.onLayout (AbsListView.java:2162) на android.view.View.layout (View.java:17663) на android.view.ViewGroup.layout (ViewGroup.java:5577) на android.widget.RelativeLayout.onLayout (RelativeLayout.java:1079) на android.view.View.layout (View.java:17663) на android.view.ViewGroup.layout (ViewGroup.java:5577) на android.widget.FrameLayout.layoutChildren (FrameLayout.java:323) на android.widget.FrameLayout.onLayout (FrameLayout.java:261) на android.view.View.layout (View.java:17663) на android.view.ViewGroup.layout (ViewGroup.java:5577) на android.support.v7.widget.ActionBarOverlayLayout.onLayout (ActionBarOverlayLayout.java:443) на android.view.View.layout (View.java:17663) на android.view.ViewGroup.layout (ViewGroup.java:5577) на android.widget.FrameLayout.layoutChildren (FrameLayout.java:323) на android.widget.FrameLayout.onLayout (FrameLayout.java:261) на android.view.View.layout (View.java:17663) на android.view.ViewGroup.layout (ViewGroup.java:5577) на android.widget.LinearLayout.setChildFrame (LinearLayout.java:1741) на android.widget.LinearLayout.layoutVertical (LinearLayout.java:1585) на android.widget.LinearLayout.onLayout (LinearLayout.java:1494) на android.view.View.layout (View.java:17663) на android.view.ViewGroup.layout (ViewGroup.java:5577) на android.widget.FrameLayout.layoutChildren (FrameLayout.java:323) на android.widget.FrameLayout.onLayout (FrameLayout.java:261) на com.android.internal.policy.DecorView.onLayout (DecorView.java:729) на android.view.View.layout (View.java:17663) на android.view.ViewGroup.layout (ViewGroup.java:5577) на android.view.ViewRootImpl.performLayout (ViewRootImpl.java:2407) на android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:2129) на android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:1315) на android.view.ViewRootImpl $ TraversalRunnable.run (ViewRootImpl.java:6435) на android.view.Choreographer $ CallbackRecord.run (Хореограф.java:876) на android.view.Choreographer.doCallbacks (Choreographer.java:688) на android.view.Choreographer.doFrame (Хореограф.java:623) на android.view.Choreographer $ FrameDisplayEventReceiver.run (Choreographer.java:862) на android.os.Handler.handleCallback (Handler.java:754) на android.os.Handler.dispatchMessage (Handler.java:95) на android.os.Looper.loop (Looper.java:163)на android.app.ActivityThread.main (ActivityThread.java:6210) в java.lang.reflect.Method.invoke (родной метод) на com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:901) на com.android.internal.os.ZygoteInit.main (ZygoteInit.java:791)

Вот мой код-

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


@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;
}

@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 later", Snackbar.LENGTH_SHORT).show();
            finish();
        }
    }
}

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

    activity_main = (RelativeLayout) findViewById(R.id.activity_main);
    fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.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("");
        }
    });


    //Check if not sign-in then navigate Signin page
    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();
        //Load content
        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) {
            //Get references to the views of list_item.xml
            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.getMessageUser());
            messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",model.getMessageTime()));

        }
    };
    listOfMessage.setAdapter(adapter);
}

}

gradle-

//Add Library
implementation 'com.android.support:design:26.1.0'
implementation 'com.firebaseui:firebase-ui:1.1.1'
implementation 'com.google.firebase:firebase-core:15.0.2'
implementation 'com.google.firebase:firebase-storage:15.0.2'
implementation 'com.google.firebase:firebase-database:15.0.1'
implementation 'com.google.firebase:firebase-auth:15.1.0' // ADDED
implementation 'com.google.android.gms:play-services-auth:15.0.1' // ADDED
implementation 'com.firebaseui:firebase-ui-auth:2.3.0'
implementation('com.facebook.android:facebook-android-sdk:4.22.1')

ChatMessage.java -

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 String getMessageText() {
    return messageText;
}

public String getMessageUser() {
    return messageUser;
}

public long getMessageTime() {
    return messageTime;
}

1 Ответ

0 голосов
/ 06 мая 2018

Вы должны добавить конструктор по умолчанию в ChatMessage.java, как показано ниже.

public class ChatMessage {
    // an empty constructor for Firebase
    public ChatMessage() {

    }

    ...
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...