gpt4 book ai didi

java.lang.RuntimeException : Layout cannot be null. 调用 setLayout

转载 作者:行者123 更新时间:2023-12-02 10:51:54 32 4
gpt4 key购买 nike

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

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lbwde.senioraid/com.example.lbwde.senioraid.MainActivity}: java.lang.RuntimeException: Layout cannot be null. Call 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>

最佳答案

更改此:

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

关于java.lang.RuntimeException : Layout cannot be null. 调用 setLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52110019/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com