0

So I'm adding an expandable fab, but I'm having this error and I don't understand why...

this is the exact error in log:

ava.lang.ClassCastException: com.nambimobile.widgets.efab.ExpandableFabLayout cannot be cast to com.nambimobile.widgets.efab.ExpandableFab at CategoryActivity.onCreate(CategoryActivity.java:56)

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;




public class CategoryActivity extends AppCompatActivity implements CategoryAdapter.onCategoryClickListener {

    public static final String CATEGORY_NAME = "category_name";

    private NoteViewModel noteViewModel;
    private CategoryAdapter adapter;
    private RecyclerView recyclerView;
    private List<FolderWithNotes> folderWithNotes;
    private Note note;
    private Folder folder;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       noteViewModel = new ViewModelProvider.AndroidViewModelFactory(this.getApplication()).create(NoteViewModel.class);

        recyclerView = findViewById(R.id.category_recyclerview);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        noteViewModel.getFolderWithNotes().observe(this, folderWithNotes -> {


       });

        ExpandableFab exFab =  findViewById(R.id.exFab);
        exFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case (R.id.exfab_add_category):
                        Intent intent = new Intent(CategoryActivity.this, AddCategoryActivity.class);
                        startActivity(intent);

                    case (R.id.exfab_delete_category):
                    default:
                        break;

                }


            }

        });



        ActivityResultLauncher<Intent> launcher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> {

            if(result.getResultCode() == Activity.RESULT_OK) {
                Intent data = result.getData();
                String category = data.getStringExtra(AddCategoryActivity.CATEGORY_REPLY);
                String notes = data.getStringExtra(AddNoteActivity.NOTE_REPLY);

                Folder folder = new Folder(note.getFolder_name());
                noteViewModel.insert(folder, note);

            }


        });

    }


    @Override
    protected void onResume() {
        super.onResume();

        adapter = new CategoryAdapter(folderWithNotes, this, this);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onCategoryClick(int position) {

        // code to transfer to AddNoteActivity (where the note list is) by clicking the category folder
        noteViewModel.getFolderWithNotes().observe(this, folderWithNotes -> {
            Intent intent = new Intent(CategoryActivity.this, NotesActivity.class);
            intent.putExtra(CATEGORY_NAME, folderWithNotes.get(position).getFolder().getFolderName());

        });


    }
    public void addCategoryClicked(View view) {
        EditText editCategory = findViewById(R.id.et_add_category);
        String categoryTitle = editCategory.getText().toString().trim();
        Intent intent = new Intent(CategoryActivity.this, AddCategoryActivity.class);


    }

    public void deleteCategoryClicked(View view) {
    }

//    @Override
//    public void addCategoryClicked(int position) {
//
//    }
//
}
XtinaG
  • 1
  • 1
  • Because you are trying to put ExpandableFabLayout in a ExpandableFab so either you have the wrong id for "findViewById(R.id.exFab)" or you are using wrong object so you should replace ExpandableFab with ExpandableFabLayout I cannot tell because you did not share the XML code. – M D Oct 15 '21 at 20:40
  • 1
    Does this answer your question? [java.lang.ClassCastException](https://stackoverflow.com/questions/3511169/java-lang-classcastexception) – Ryan M Oct 19 '21 at 03:06
  • Hey, I'm the creator of the ExpandableFab library. @MD is right on the money, and to give more context: `ExpandableFabLayout` is a `ViewGroup` - it derives from `CoordinatorLayout`. It is the layout that holds the other children Views of the widget, like `Overlay`, `FabOption`, `Label` and `ExpandableFab`. So I think your View with ID of `exFab` is your parent layout (that is, the ExpandableFabLayout). You need to give an ID to your ExpandableFab View (which is a child inside your layout), and then assign that to your exFab variable. To be sure, please update this with your XML layout file. – kabumere Oct 21 '21 at 02:22

0 Answers0