Abd*_*gun 1 java android firebase google-cloud-firestore
我是android编程的新手。我有一个可以插入到云 Firestore 中的应用程序,它还需要读取 Cloud Firestore 中的数据。当我尝试打开读取数据的活动时,它一直崩溃。logcat 给出了这个错误
2021-01-12 21:20:56.733 25524-25524/com.example.ppw1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ppw1, PID: 25524
java.lang.IllegalStateException: ViewHolder views must not be attached when created. Ensure that you are not passing 'true' to the attachToRoot parameter of LayoutInflater.inflate(..., boolean attachToRoot)
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7080)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1897)
at androidx.recyclerview.widget.RecyclerView$1.run(RecyclerView.java:414)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:616)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Run Code Online (Sandbox Code Playgroud)
我的代码是:
2021-01-12 21:20:56.733 25524-25524/com.example.ppw1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ppw1, PID: 25524
java.lang.IllegalStateException: ViewHolder views must not be attached when created. Ensure that you are not passing 'true' to the attachToRoot parameter of LayoutInflater.inflate(..., boolean attachToRoot)
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7080)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1897)
at androidx.recyclerview.widget.RecyclerView$1.run(RecyclerView.java:414)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:616)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Run Code Online (Sandbox Code Playgroud)
我的 DoToAdapter 代码正在处理大部分查询,我认为错误必须是这些类之一,但我不确定。
package com.example.ppw1;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import android.os.Bundle;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
public class ListActivities extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference todoRef = db.collection("users");
private ToDoAdapter adapter;
private RecyclerView mFirestoreList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_activities);
this.setTitle("List Activity");
mAuth = FirebaseAuth.getInstance();
setUpRecyclerView();
mFirestoreList = findViewById(R.id.firestore_list);
}
private void setUpRecyclerView()
{
Query query = todoRef.whereEqualTo("Email", mAuth.getCurrentUser().getEmail());
FirestoreRecyclerOptions<ToDoItem> options = new FirestoreRecyclerOptions.Builder<ToDoItem>().setQuery(query,ToDoItem.class).build();
adapter = new ToDoAdapter(options);
RecyclerView recyclerView = findViewById(R.id.firestore_list);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
@Override
protected void onStart()
{
super.onStart();
adapter.startListening();
}
@Override
protected void onStop()
{
super.onStop();
adapter.stopListening();
}
}Run Code Online (Sandbox Code Playgroud)
java.lang.IllegalStateException:创建时不得附加 ViewHolder 视图。确保您没有将“true”传递给 LayoutInflater.inflate(..., boolean attachToRoot) 的 attachToRoot 参数
attachToRoot如果ViewGroup参数不是,则默认值为真,null请参见2-arg inflate 方法:
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
return inflate(resource, root, root != null);
}
Run Code Online (Sandbox Code Playgroud)
因此,当您在其中膨胀项目布局时,您需要使用3-arginflate()方法将第三个attachToRoot参数设置为falseonCreateViewHolder()
@NonNull
@Override
public ToDoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_to_do_item, parent, false);
return new ToDoHolder(v);
}
Run Code Online (Sandbox Code Playgroud)
attachToRoot膨胀的层次结构是否应该附加到根参数?如果为 false,则 root 仅用于为 XML 中的根视图创建正确的 LayoutParams 子类。
| 归档时间: |
|
| 查看次数: |
115 次 |
| 最近记录: |