gpt4 book ai didi

Android - 无法在 Fragment 中使用弹出窗口

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:49:40 26 4
gpt4 key购买 nike

我是安卓新手。现在我正在尝试创建一个 slider 。现在我已经创建了一个滑动条。现在我想在单击操作栏中的图标时显示弹出窗口。当我扩展 Activity 时,弹出窗口工作正常。但是,当我更改为 extents Fragment 时,我无法使用弹出窗口。请让我知道 fragment 页面中弹出窗口的任何想法或示例。

public void popup_window() {
View menuItemView = findViewById(R.id.menu_popup);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.getMenuInflater().inflate(R.menu.list_, popupMenu.getMenu());

popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_ticket:
Intent intdn = new Intent(List_Activity.this,List_Activity.class);
intdn.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intdn);
break;

case R.id.action_survey:
Toast.makeText(getApplicationContext(),"Under Construction ",Toast.LENGTH_LONG).show();
break;
case R.id.action_service_request:
Toast.makeText(getApplicationContext(),"Under Construction ",Toast.LENGTH_LONG).show();
break;

default:
break;

}
return true;
}
});
popupMenu.show();
}

我收到错误:- Error Image

很多错误。请帮我解决这个问题。提前致谢。

LogCat 错误信息:-

    FATAL EXCEPTION: main
java.lang.IllegalStateException: MenuPopupHelper cannot be used without an anchor
at com.android.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:101)
at android.widget.PopupMenu.show(PopupMenu.java:108)
at com.example.sample.Testing_Fragment1.popup_window(Testing_Fragment1.java:262)
at com.example.sample.Testing_Fragment1.onOptionsItemSelected(Testing_Fragment1.java:227)
at android.app.Fragment.performOptionsItemSelected(Fragment.java:1801)
at android.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java:1959)
at android.app.Activity.onMenuItemSelected(Activity.java:2551)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

When I extents activity the popup is working fine. But when I change into extents Fragment I can not use the popup.

您可以直接为 Activity 调用 findViewById(),但是当您使用 Fragment 时,您将需要一个 View 对象来调用通过 Id 查找 View ()。例如。 getView().findViewById();

  1. View - findViewById()

  2. Activity - findViewById()

    getView() --这将返回 fragment 的 Root View ,您可以用它调用 findViewById()

new PopupMenu(this, menuItemView);

此处弹出菜单需要 Context,作为第一个参数传递。如果您处于 Activity 状态,则可以使用 this,但是在 Fragment 中,您需要使用 getActivity() 而不是 this

PopupMenu(Context context, View anchor)

new Intent(List_Activity.this,List_Activity.class);

错了,其实应该是package context和class

packageContext --- 实现此类的应用程序包的上下文。

class --- 用于 Intent 的组件类。

每当您想显示 Fragment< 中的 Toast 时,请使用 getActivity().getApplicationContext() 而不仅仅是 getApplicationContext()/

所以你的代码看起来像

public void popup_window() {

View menuItemView = getView().findViewById(R.id.menu_popup);
PopupMenu popupMenu = new PopupMenu(getActivity(), menuItemView);
popupMenu.getMenuInflater().inflate(R.menu.list_, popupMenu.getMenu());

popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_ticket:
Intent intdn = new Intent(getActivity(),List_Activity.class); // Your nxt activity name instead of List_Activity
intdn.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
getActivity().startActivity(intdn);
break;

case R.id.action_survey:
Toast.makeText(getActivity().getApplicationContext(),"Under Construction ",Toast.LENGTH_LONG).show();
break;
case R.id.action_service_request:
Toast.makeText(getActivity().getApplicationContext(),"Under Construction ",Toast.LENGTH_LONG).show();
break;

default:
break;

}
return true;
}
});
popupMenu.show();
}

更新:

java.lang.IllegalStateException: MenuPopupHelper cannot be used without an anchor

你得到这个异常是因为我猜这个弹出窗口的 anchor View 是空的。因此,每当系统尝试显示弹出窗口时,它都会给您这个异常。

只需检查 tryShow() 中的 MenuPopupHelper

另请参阅 this post关于 Maxim Zaslavsky

的 SO

关于Android - 无法在 Fragment 中使用弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23539792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com