NavigationDrawer不能强制转换为Interface



我试图实现一个在片段之间传递数据的接口,但我被困在了这里,我在回收器视图上进行点击监听器,当有选择时,我希望值显示在另一个片段中

2020-10-16 00:17:21.541 21250-21250/com.example.iszo_dev E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.iszo_dev, PID: 21250
java.lang.ClassCastException: com.example.iszo_dev.Activity.NavigationDrawer cannot be cast to com.example.iszo_dev.Interface.Communicator
at com.example.iszo_dev.Activity.ui.home.MenuCardFragment$2.onBindViewHolder(MenuCardFragment.java:197)
at com.example.iszo_dev.Activity.ui.home.MenuCardFragment$2.onBindViewHolder(MenuCardFragment.java:161)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:122)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
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.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1762)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at androidx.recyclerview.widget.RecyclerView$LayoutManager.layoutDecoratedWithMargins(RecyclerView.java:9587)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1685)
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.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at androidx.viewpager2.widget.ViewPager2.onLayout(ViewPager2.java:527)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1735)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1762)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
2020-10-16 00:17:21.542 21250-21250/com.example.iszo_dev E/AndroidRuntime:     at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:148)
at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:43)
at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1996)
at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:918)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1735)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1982)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1826)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1735)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:383)
at android.widget.FrameLayout.onLayout(FrameLayout.java:321)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:751)
at android.view.View.layout(View.java:17838)
at android.view.ViewGroup.layout(ViewGroup.java:5754)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2711)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2384)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1462)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6965)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:907)
at android.view.Choreographer.doCallbacks(Choreographer.java:709)
at android.view.Choreographer.doFrame(Choreographer.java:644)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:893)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

和:

public class MenuCardFragment extends Fragment {
...

protected void onBindViewHolder(@NonNull final MenuCardFragment.ItemViewHolder itemViewHolder, int position, @NonNull final Items item) {

String categorie = getArguments().getString("categorie");
final String foodType = item.getFoodType();

if (shop_type == null) {
shop_type = "Shop";
}

final String itemName = item.getItemName();
final String itemPrice = item.getItemPrice();
final String itemType = item.getItemType();
final String btw = item.getBtw_tarif();

if (itemPrice.equals("sub")) {
itemViewHolder.makeSubCategoryRow(itemName);
itemViewHolder.mView.findViewById(R.id.subcategory_row_text).setVisibility(View.VISIBLE);
itemViewHolder.mView.findViewById(R.id.item_sub).setVisibility(View.GONE);
itemViewHolder.mView.findViewById(R.id.item_single_amount).setVisibility(View.GONE);
itemViewHolder.mView.findViewById(R.id.single_display_nr).setVisibility(View.GONE);
itemViewHolder.mView.findViewById(R.id.item_display_single_name).setVisibility(View.GONE);
itemViewHolder.mView.findViewById(R.id.item_single_amount).setVisibility(View.GONE);
itemViewHolder.mView.findViewById(R.id.item_display_single_price).setVisibility(View.GONE);
return;
}
itemViewHolder.mView.findViewById(R.id.subcategory_row_text).setVisibility(View.GONE);

final Communicator communicator = (Communicator)getActivity();
itemViewHolder.setRowNumber(position);
itemViewHolder.setItemName(itemName);
itemViewHolder.setItemPrice(itemPrice);

itemViewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
communicator.sendToBill(itemName, itemPrice, foodType, itemType, btw);
itemViewHolder.updateAmountField();

}
});
itemViewHolder.mView.findViewById(R.id.item_sub).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemViewHolder.updateAmountFieldForRemove();
}
});
}
};


}

问题是:

java.lang.ClassCastException: com.example.iszo_dev.Activity.NavigationDrawer cannot be cast to com.example.iszo_dev.Interface.Communicator at

取决于final Communicator communicator = (Communicator)getActivity();

Activity必须扩展或实现Communicator

相关内容

  • 没有找到相关文章

最新更新