skydoves / PowerMenu

:fire: Powerful and modernized popup menu with fully customizable animations.
Apache License 2.0
1.19k stars 174 forks source link

powerMenu.isShowing() method null pointer exeption. #37

Closed uzaysan closed 6 years ago

uzaysan commented 6 years ago

public class GuestProfileFragment extends Fragment {
    PowerMenu powerMenu;

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_guest_profile, container, false);

       powerMenu = new PowerMenu.Builder(getContext())
                .addItem(new PowerMenuItem(getString(R.string.block), false))
                .addItem(new PowerMenuItem(getString(R.string.report), false))
                .setAnimation(MenuAnimation.SHOWUP_TOP_RIGHT)
                .setMenuRadius(10f)
                .setMenuShadow(10f)
                .setTextColor(getResources().getColor(R.color.md_grey_800))
                .setSelectedTextColor(Color.WHITE)
                .setMenuColor(Color.WHITE)
                .setSelectedMenuColor(getResources().getColor(R.color.colorPrimary))
                .setOnMenuItemClickListener(new OnMenuItemClickListener<PowerMenuItem>() {
                    @Override
                    public void onItemClick(int position, PowerMenuItem item) {
                        Toast.makeText(getContext(),item.getTitle(),Toast.LENGTH_SHORT).show();
                        powerMenu.dismiss();
                    }
                })
                .build();

        return view;
    }

public void backpress(){
        if(powerMenu.isShowing()){
            powerMenu.dismiss();
        }
        else{
            getFragmentManager().popBackStack();
        }
    }

}

This code give me null pointer exception how to fix this?

I saw your code u used PowerMenuUtil class but that class doesnt show up .

one more thing

this is the only dependency i use implementation "com.github.skydoves:powermenu:2.0.5"

skydoves commented 6 years ago

@uzaysan Hello, I think getContext() at onCreateView method returns null. It seems better to initialize PowerMenu at onAttach method. Thank you!

uzaysan commented 6 years ago
@Override
    public void onAttach(Context context) {

        super.onAttach(context);
        powerMenu = new PowerMenu.Builder(context)
                .addItem(new PowerMenuItem(getString(R.string.block), false))
                .addItem(new PowerMenuItem(getString(R.string.report), false))
                .setAnimation(MenuAnimation.SHOWUP_TOP_RIGHT)
                .setMenuRadius(10f)
                .setMenuShadow(10f)
                .setTextColor(getResources().getColor(R.color.md_grey_800))
                .setSelectedTextColor(Color.WHITE)
                .setMenuColor(Color.WHITE)
                .setSelectedMenuColor(getResources().getColor(R.color.colorPrimary))
                .setOnMenuItemClickListener(new OnMenuItemClickListener<PowerMenuItem>() {
                    @Override
                    public void onItemClick(int position, PowerMenuItem item) {
                        Toast.makeText(getContext(),item.getTitle(),Toast.LENGTH_SHORT).show();
                        powerMenu.dismiss();
                    }
                })
                .build();

    }

still same error

skydoves commented 6 years ago

@uzaysan Are that codes working well in Activity?

uzaysan commented 6 years ago

Yes its working well in activity.

@skydoves

skydoves commented 6 years ago

@uzaysan It's working well in Fragment. Check below codes please :0

PowerMenuUtils

public class PowerMenuUtils {

    public static PowerMenu getHamburgerPowerMenu(Context context, LifecycleOwner lifecycleOwner, OnMenuItemClickListener<PowerMenuItem> onMenuItemClickListener, OnDismissedListener onDismissedListener) {
        return new PowerMenu.Builder(context)
                .addItem(new PowerMenuItem("Novel", true))
                .addItem(new PowerMenuItem("Poetry", false))
                .addItem(new PowerMenuItem("Art", false))
                .addItem(new PowerMenuItem("Journals", false))
                .addItem(new PowerMenuItem("Travel", false))
                .setAutoDismiss(true)
                .setLifecycleOwner(lifecycleOwner)
                .setAnimation(MenuAnimation.SHOWUP_TOP_LEFT)
                .setMenuRadius(10f)
                .setMenuShadow(10f)
                .setTextColor(context.getResources().getColor(R.color.md_grey_800))
                .setSelectedTextColor(Color.WHITE)
                .setMenuColor(Color.WHITE)
                .setSelectedMenuColor(context.getResources().getColor(R.color.colorPrimary))
                .setOnMenuItemClickListener(onMenuItemClickListener)
                .setOnDismissListener(onDismissedListener)
                .build();
    }
}

TestFragment.class

public class TestFragment extends Fragment {

    private PowerMenu powerMenu;
    private View parent;

    private OnMenuItemClickListener<PowerMenuItem> onHamburgerItemClickListener = new OnMenuItemClickListener<PowerMenuItem>() {
        @Override
        public void onItemClick(int position, PowerMenuItem item) {
            Toast.makeText(getContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
            powerMenu.setSelectedPosition(position);
        }
    };

    private OnDismissedListener onHamburgerMenuDismissedListener = new OnDismissedListener() {
        @Override
        public void onDismissed() {
            Log.d("Test", "onDismissed hamburger menu");
        }
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        parent = inflater.inflate(R.layout.fragment_test, container, false);
        return parent;
    }

    @Override
    public void onAttach(final Context context) {
        super.onAttach(context);
        powerMenu = PowerMenuUtils.getHamburgerPowerMenu(getContext(), this, onHamburgerItemClickListener, onHamburgerMenuDismissedListener);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Button button = parent.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                powerMenu.showAsDropDown(view);
                Toast.makeText(getContext(), powerMenu.isShowing() + "", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

fragment_test.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="button"/>
</RelativeLayout>
uzaysan commented 6 years ago

Still same error isShowing MEthod Return null.

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.skydoves.powermenu.PowerMenu.isShowing()' on a null object reference

skydoves commented 6 years ago

@uzaysan Could you create a public repository for the issue example project? It seems PowerMenu instance is not initialized properly.

uzaysan commented 6 years ago

@skydoves

https://github.com/uzaysan/exampleProject/blob/master/MyApplication.zip

This is pretty much how i use fragments and power menu.

skydoves commented 6 years ago

@uzaysan You should change below codes.

MainActivity.class

@Override
    public void onBackPressed() {
        if(getSupportFragmentManager().getBackStackEntryCount() > 0) {
            if(getCurrentFragment() instanceof PowerMenuFragment) {
                PowerMenuFragment powerMenuFragment = new PowerMenuFragment();
                powerMenuFragment.backPressed();
            }
        }
        else{
            super.onBackPressed();
        }
    }

to

*MainActivity.class

@Override
    public void onBackPressed() {
        if(getSupportFragmentManager().getBackStackEntryCount() > 0) {
            if(getCurrentFragment() instanceof PowerMenuFragment) {
                PowerMenuFragment powerMenuFragment = (PowerMenuFragment) getCurrentFragment();
                powerMenuFragment.backPressed();
            }
        }
        else{
            super.onBackPressed();
        }
    }

You create new instance of PowerMenuFragment.class. But It is not attached on activity. So the powermenu field is not be initialized.

uzaysan commented 6 years ago

Yes It works now thanks