-2

The app crashes as soon as i press button to send message via firebase. I dont know how to fix this error. I tried alot but it won't stop crashing.

I have 2 layout files and those are list_item2.xml and listitem_chatbox.xml.

I have project submission tonight, so any help real soon will be highly appreciated, Thnkyou

public class MainActivityChat extends AppCompatActivity{


private static int SIGN_IN_REQUEST_CODE=1;



Query query = FirebaseDatabase.getInstance()
        .getReference()
        .child("chats")
        .limitToLast(50);

FirebaseRecyclerOptions<ChatMessage> options =
        new FirebaseRecyclerOptions.Builder<ChatMessage>()
                .setQuery(query, ChatMessage.class)
                .build();



private FirebaseListAdapter<ChatMessage> adapter;
RelativeLayout listitem_chatbox;
FloatingActionButton fab;


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == R.id.nav_logout)
    {
        AuthUI.getInstance().signOut(this).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
           Snackbar.make(listitem_chatbox, "YOU HAVE BEEN LOGGED OUT", Snackbar.LENGTH_SHORT).show();
           finish();
            }
        });
    }

    return true;

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == SIGN_IN_REQUEST_CODE)
    {
        Snackbar.make(listitem_chatbox, "SUCCESSFULLY SIGNED IN, WELCOME!", Snackbar.LENGTH_SHORT).show();
        displayChatMessage();
    }

    else
    {
        Snackbar.make(listitem_chatbox, "SIGN IN FAILED, TRY LATER!!", Snackbar.LENGTH_SHORT).show();
        finish();
    }
}

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

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


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

        //display content
        displayChatMessage();

    }


}

private void displayChatMessage() {


      RecyclerView RV = (RecyclerView) findViewById(R.id.rv_numbers);
      LinearLayoutManager LLM = new LinearLayoutManager(this);
      RV.setLayoutManager(LLM);
      RV.setHasFixedSize(true);

    class ChatHolder extends RecyclerView.ViewHolder
                {

        TextView messageText, messageUser, messageTime;

        public ChatHolder(View itemView) {
            super(itemView);


            messageText = (TextView) findViewById(R.id.message_text);
            messageUser = (TextView) findViewById(R.id.message_user);
            messageTime = (TextView) findViewById(R.id.message_time);


        }


    }
    FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<ChatMessage, ChatHolder>(options) {


        @Override
        public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // Create a new instance of the ViewHolder, in this case we are using a custom
            // layout called R.layout.message for each item
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_item2, parent, false);

            return new ChatHolder(view);
        }

        @Override
        protected void onBindViewHolder(ChatHolder holder, int position, ChatMessage model) {
            // Bind the Chat object to the ChatHolder
            // ...
            holder.messageText.setText(model.getMessageText());
            holder.messageUser.setText(model.getMessageUser());
            holder.messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", model.getMessageTime()));
        }

        @Override
        public int getItemCount() {
            return super.getItemCount();
        }


    };
    RV.setAdapter(adapter);
}

}

  • how to correct it? @Selvin, i'd appreciate it, Thankyou – Umer Saleem Nov 25 '17 at 12:41
  • Pass right parameters to constructor... Which are right? It is all in the documentation – Selvin Nov 25 '17 at 12:43
  • I am passing the parameters which i think are correct, but they are actually not, so i guess i'd be needing a little help with the code. I've tried re-arranging the parameters too but it didnt work. – Umer Saleem Nov 25 '17 at 12:50
  • The constructor is `public FirebaseListAdapter(FirebaseListOptions options)` – Selvin Nov 25 '17 at 13:00

1 Answers1

1

You seem to be using FirebaseUI 3.x, while your code is using the syntax for FirebaseUI 2.x and lower. In 3.x the constructors for the adapter were changed to take an FirebaseRecyclerOptions argument instead of separate parameters. From the readme on github:

First, configure the adapter by building FirebaseRecyclerOptions. In this case we will continue with our chat example:

FirebaseRecyclerOptions<Chat> options =
            new FirebaseRecyclerOptions.Builder<Chat>()
                    .setQuery(query, Chat.class)
                    .build();

Next create the FirebaseRecyclerAdapter object. You should already have a ViewHolder subclass for displaying each item. In this case we will use a custom ChatHolder class:

adapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
  • Thankyou, well that helped and my errors were removed. But my app crashes now as soon as i press on send message button. Caused by: java.lang.RuntimeException: Layout cannot be null. Call setLayout – Umer Saleem Nov 26 '17 at 08:25
  • Good to hear that the initial problem is gone. That seems like a different problem, and [this should help](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). If you can't fix it, I recommend isolating that problem and asking a separate question about it. If you spend the time [creating an MCVE](http://stackoverflow.com/help/mcve), you are much more likely to get help and much less likely to get downvoted. – Frank van Puffelen Nov 26 '17 at 16:10