java.lang.RuntimeException:布局不能为null。调用setLayout

时间:2018-08-31 06:53:59

标签: android firebase firebase-realtime-database firebaseui

运行聊天应用程序时,我不断收到此错误:

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.lbwde.senioraid / com.example.lbwde.senioraid.MainActivity}:java.lang.RuntimeException:布局不能为null。调用setLayout。

似乎链接到此代码:

   private void displayChat() {

        ListView listOfMessage = findViewById(R.id.list_of_message);


        Query query = FirebaseDatabase.getInstance().getReference();
        FirebaseListOptions<Chat> options = new FirebaseListOptions.Builder<Chat>()
                .setQuery(query, Chat.class)
                .build();

        adapter = new FirebaseListAdapter<Chat>(options) {
            @Override
            protected void populateView(View v, Chat model, int position) {
                //Get reference to the views of list_item.xml
                TextView messageText, messageUser, messageTime;
                messageText = findViewById(R.id.message_text);
                messageUser = findViewById(R.id.message_user);
                messageTime = 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);
    }

我似乎无法弄清楚为什么。代码中没有任何错误,尽管我只是更新了该节以使其可以在Firebase 3+中正常工作。

这是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_main"
    tools:context=".MainActivity">


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:clickable="true"
        android:src="@drawable/ic_send"
        android:tint="@android:color/white"
        app:fabSize="mini"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/fab"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"></android.support.design.widget.TextInputLayout>

        <EditText
        android:id="@+id/input"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginEnd="284dp"
        android:layout_marginRight="284dp"
        android:hint="Message..."
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />


    <ListView
        android:id="@+id/list_of_message"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_above="@+id/fab"
        android:dividerHeight="16dp"
        android:divider="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</android.support.constraint.ConstraintLayout>

还有我的list_item布局文件:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:id="@+id/message_user"
        android:textStyle="normal|bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_alignParentBottom="@+id/message_user"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:id="@+id/message_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="5dp"
        android:layout_below="@id/message_user"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textSize="18sp"
        android:id="@+id/message_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

更改此:

Query query = FirebaseDatabase.getInstance().getReference();
    FirebaseListOptions<Chat> options = new FirebaseListOptions.Builder<Chat>()
            .setQuery(query, Chat.class)
            .build();

对此:

 Query query = FirebaseDatabase.getInstance().getReference();
    FirebaseListOptions<Chat> options = new FirebaseListOptions.Builder<Chat>()
            .setLayout(R.layout.row) //name of the row xml file
            .setQuery(query, Chat.class)
            .build();

检查此:

https://github.com/firebase/FirebaseUI-Android/blob/master/database/src/main/java/com/firebase/ui/database/FirebaseListOptions.java#L140

https://github.com/firebase/FirebaseUI-Android/blob/master/database/src/main/java/com/firebase/ui/database/FirebaseListOptions.java#L162

答案 1 :(得分:0)

您没有在适配器中设置布局。

设置列表项的布局

FirebaseListOptions<Chat> options = new FirebaseListOptions.Builder<Chat>()
                .setQuery(query, Chat.class)
                .setLayout(R.layout.list_item)
                .build();

然后初始化您的TextViews:

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