引起:java.lang.RuntimeException:布局不能为null。致电setLayout

时间:2017-11-25 12:34:46

标签: android firebase

当我按下按钮通过firebase发送消息时,应用程序崩溃。我不知道如何解决这个错误。我尝试了很多,但它不会停止崩溃。

我有2个布局文件,分别是list_item2.xml和listitem_chatbox.xml。

我今晚有项目提交,所以很快就会有任何帮助,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);
}

}

1 个答案:

答案 0 :(得分:1)

您似乎使用的是FirebaseUI 3.x,而您的代码使用的是FirebaseUI 2.x及更低版本的语法。在3.x中,适配器的构造函数被更改为采用FirebaseRecyclerOptions参数而不是单独的参数。来自readme on github

  

首先,通过构建FirebaseRecyclerOptions来配置适配器。在这种情况下,我们将继续我们的聊天示例:

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

接下来创建FirebaseRecyclerAdapter对象。您应该已经有一个ViewHolder子类来显示每个项目。在这种情况下,我们将使用自定义ChatHolder类:

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