Closed uzaysan closed 5 years ago
@uzaysan
Hello,
I think getContext()
at onCreateView
method returns null.
It seems better to initialize PowerMenu
at onAttach
method.
Thank you!
@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
@uzaysan Are that codes working well in Activity?
Yes its working well in activity.
@skydoves
@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>
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
@uzaysan Could you create a public repository for the issue example project? It seems PowerMenu instance is not initialized properly.
@skydoves
https://github.com/uzaysan/exampleProject/blob/master/MyApplication.zip
This is pretty much how i use fragments and power menu.
@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.
Yes It works now thanks
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"