【问题标题】:Android Studio & Java : No layout manager attached; Skipping layoutAndroid Studio 和 Java:没有附加布局管理器;跳过布局
【发布时间】:2022-06-11 09:43:54
【问题描述】:

我完全被这个错误迷失了,代码:

适配器:

package com.example.bookapp;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageButton;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;
    
    import com.example.bookapp.databinding.RowCategoryBinding;
    
    import java.util.ArrayList;
    
    public class AdapterCategory extends RecyclerView.Adapter<AdapterCategory.HolderCategory>{
    
        private final Context context;
        private final ArrayList<ModelCategory> categoryArrayList;
    
        //view binding
        private RowCategoryBinding binding;
    
        public AdapterCategory(Context context, ArrayList<ModelCategory> categoryArrayList) {
            this.context = context;
            this.categoryArrayList = categoryArrayList;
        }
    
        @NonNull
        @Override
        public HolderCategory onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            binding = RowCategoryBinding.inflate(LayoutInflater.from(context), parent,false);
    
            return new HolderCategory(binding.getRoot());
        }
    
        @Override
        public void onBindViewHolder(@NonNull HolderCategory holder, int position) {
            //get data
            ModelCategory model = categoryArrayList.get(position);
            String id = model.getId();
            String category = model.getCategory();
            String uid = model.getUid();
            long timestamp = model.getTimestamp();
    
            //set data
            holder.categoryTv.setText(category);
    
            //handle click, delete category
            holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, ""+category, Toast.LENGTH_SHORT).show();
    
                }
            });
    
    
        }
    
        @Override
        public int getItemCount() {
            return categoryArrayList.size();
        }
    
    
        /*View holder class to hold UI views for row_category.xml*/
        class HolderCategory extends RecyclerView.ViewHolder{
    
            //ui views of row_category
            TextView categoryTv;
            ImageButton deleteBtn;
    
    
            public HolderCategory(View v){
                super(v);
    
                //init ui views
                categoryTv = binding.categoryTv;
                deleteBtn = binding.deleteBtn;
            }
        }
    }

行类别:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:contentPadding="10dp"
    app:cardUseCompatPadding="true"
    app:cardBackgroundColor="@color/white"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">



        <TextView
            android:id="@+id/categoryTv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/category_title"
            android:layout_weight="1"
            android:textColor="@color/black"/>

        <ImageButton
            android:id="@+id/deleteBtn"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/ic_baseline_delete_forever_24"
            android:background="@drawable/shape_button03"



            />

    </LinearLayout>

</androidx.cardview.widget.CardView>

模型类别: 包 com.example.bookapp;

public class ModelCategory {
    String id, category, uid;
    long timestamp;

    //constructor empty req for firebase
    public ModelCategory(){

    }

    //parametrized constructor

    public ModelCategory(String id, String category, String uid, long timestamp) {
        this.id = id;
        this.category = category;
        this.uid = uid;
        this.timestamp = timestamp;
    }

    /*----Getter/Setters----*/

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}

DashboardAdminActivity:

package com.example.bookapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.example.bookapp.databinding.ActivityDasboardBinding;
import com.example.bookapp.databinding.ActivityDashboardAdminBinding;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.Objects;

public class DashboardAdminActivity extends AppCompatActivity {

    //view binding
    private ActivityDashboardAdminBinding binding;
    //firebase auth
    private FirebaseAuth firebaseAuth;

    //arraylist to store category
    private ArrayList<ModelCategory> categoryArrayList;
    //adapter
    private AdapterCategory adapterCategory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityDashboardAdminBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //init firebase auth
        firebaseAuth = FirebaseAuth.getInstance();
        checkUser();

        //handle click, logout
        binding.logOutBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                firebaseAuth.signOut();
                checkUser();
                loadCategories();
            }
        });

        //handle click, go to add category
        binding.addCategoryBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(DashboardAdminActivity.this,CategoryAddActivity.class));
            }
        });

    }

    private void loadCategories() {
        //init arraylist
        categoryArrayList = new ArrayList<>();

        //get all categories from firebase > Categories
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Categories");
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                //clear arraylist before adding data info it
                categoryArrayList.clear();
                for (DataSnapshot ds: snapshot.getChildren()){
                    //get data
                    ModelCategory model = ds.getValue(ModelCategory.class);

                    //add to arraylist
                    categoryArrayList.add(model);
                }
                //setup adapter
                adapterCategory = new AdapterCategory(DashboardAdminActivity.this, categoryArrayList);
                //set adapter tp recyclerView
                binding.categoriesRv.setAdapter(adapterCategory);



            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }

    private void checkUser() {
        //get current user
        FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
        if(firebaseUser==null){
            //not logged inm go to main screen
            startActivity(new Intent(this,LoginActivity.class));
            finish();
        }
        else{
            //logged in, user info
            String email = firebaseUser.getEmail();
            //set in textview of toolbar
            binding.subTitleTv.setText(email);

        }
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/back01"
    tools:context=".DashboardAdminActivity">
<!--Toolbar-->
    <RelativeLayout
        android:id="@+id/toolbarRl"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@drawable/shape_toolbar02">



        <TextView
            android:id="@+id/titleTv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_centerHorizontal="true"
            android:text="Dashboard Admin"
            android:textStyle="bold"
            android:textColor="@color/white"/>
        <TextView
            android:id="@+id/subTitleTv"
            android:layout_width="wrap_content"
            android:layout_below="@+id/titleTv"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="abx@example.com"
            android:textColor="@color/white"/>

        <ImageButton
            android:id="@+id/logOutBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:src="@drawable/ic_baseline_power_settings_new_24"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="10dp"
            android:background="@android:color/transparent"/>



    </RelativeLayout>
<!--Categories-->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/categoriesRv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottomLl"
        android:layout_below="@id/toolbarRl"
        android:layout_margin="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:listitem="@layout/row_category" />
    <LinearLayout
        android:id="@+id/bottomLl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:orientation="horizontal">

        <!--Add Category-->
        <Button
            android:id="@+id/addCategoryBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Category"
            android:layout_weight="1"
            android:layout_marginEnd="10dp"
            android:background="@drawable/shape_button"
            android:minHeight="50dp"
            android:textAllCaps="false"
            android:textColor="@color/white"/>

        <!--Add PDF-->
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/addPdfFab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_baseline_picture_as_pdf_24"
            android:tint="@color/white"
            app:fabCustomSize="50dp"
            android:backgroundTint="@color/yellow"/>



    </LinearLayout>

</RelativeLayout>

使用 Firebase 制作电子书应用。 数据不显示。 知道有什么问题吗? 我已经阅读了与同一问题相关的其他问题,但没有一个有帮助。

【问题讨论】:

  • No layout manager attached; Skipping layout 已经作为问题存在,添加 Android Studio &amp; Java : 以便发布您的问题并不意味着它与之前提出的问题不同 - 为什么要在 onDataChange 内重复创建适配器?创建并分配一次,在 onDataChange 中更改该适配器的数据
  • @Gardener 据我所知已经在 xml 上设置了,所以也不需要在活动中添加它
  • 如果您遇到问题,最好在发布问题时创建MCVE。您为此问题发布了近 400(四百)行代码。人们需要解析和尝试在线调试的内容很多。请编辑您的问题并隔离问题,这样可以增加获得帮助的机会。

标签: java android firebase


猜你喜欢
  • 1970-01-01
  • 2020-07-27
  • 2017-12-01
  • 1970-01-01
  • 2021-09-19
  • 2018-06-12
  • 2021-12-19
  • 1970-01-01
相关资源
最近更新 更多