I have an ActivityController that contains a bottom navigation and I want to create interface between RecyclerviewAdapter and Fragment that are inside the bottom navigation to send the adapter position.
I get this error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.my.money, PID: 10644
java.lang.ClassCastException: com.my.money.Content.ControllerActivity cannot be cast to com.my.money.Content.ui.debt.AdapterInterface
at com.my.money.Content.ui.debt.DebtFragment.onCreateView(DebtFragment.java:52)
DebtFragment :
public class DebtFragment extends Fragment implements View.OnClickListener, AdapterInterface {
private FragmentDebtBinding binding;
private ClassAdapterDebt adapter;
private ArrayList<CustomListDebt> arrayList = new ArrayList<>();
private Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentDebtBinding.inflate(inflater, container, false);
View view = binding.getRoot();
binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new ClassAdapterDebt(arrayList, getActivity(), (AdapterInterface) getActivity());
binding.recyclerView.setAdapter(adapter);
showAllData();
return view;
}
...
@Override
public void onClickItem(int position) {
Toast.makeText(getContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
}
Interface
public interface AdapterInterface {
void onClickItem(int position);
}
Adapter
private ArrayList<CustomListDebt> arrayList = new ArrayList<>();
private Context context;
private AdapterInterface adapterInterface;
// Constructor
public ClassAdapterDebt(ArrayList<CustomListDebt> arrayList, Context context, AdapterInterface anInterface) {
this.arrayList = arrayList;
this.context = context;
this.adapterInterface = anInterface;
}
This in onBindViewHolder
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (adapterInterface != null) {
adapterInterface.onClickItem(position);
} else {
adapterInterface.onClickItem(0);
}
}
});
My try
I try to change this
adapter = new ClassAdapterDebt(arrayList, getActivity(), (AdapterInterface) getActivity());
To this :
adapter = new ClassAdapterDebt(arrayList, getActivity(), (AdapterInterface) context);
Error cannot cast will disappear and replaced with error :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.my.money, PID: 22316
java.lang.NullPointerException: Attempt to invoke interface method 'void com.my.money.Content.ui.debt.AdapterInterface.onClickItem(int)' on a null object reference
at com.my.money.Content.ui.debt.ClassAdapterDebt$1.onClick(ClassAdapterDebt.java:68)
There are some notes during the build proccess, does this have any effect?
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: C:\Users\asus\AndroidStudioProjects\PencatatKeuangan\app\src\main\java\com\my\money\Content\ui\Adapter\RecyclerHomeAdapter.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Any help will be very important