gpt4 book ai didi

android - 如何创建像谷歌搜索小部件中的弹出菜单?

转载 作者:行者123 更新时间:2023-11-29 00:42:55 25 4
gpt4 key购买 nike

我正在为 Android 创建一个类似于 Google 搜索小部件的小部件。

enter image description here

他们如何在点击过滤器时实现快速操作弹出窗口? (最左边带有'g'标志的项目)

这是它的代码 -在我的应用程序小部件提供程序中,我有以下内容 -

Intent activityIntent = new Intent(context, SearchActivity.class);
Bundle infoBundle = new Bundle();
infoBundle.putBoolean(HOME_BUTTON_CLICKED, true);
activityIntent.putExtras(infoBundle);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent homeButtonPI = PendingIntent.getActivity(context, requestCode, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.homeButton, homeButtonPI);

在我的 SearchActivity 中,我的 onCreate() 中有这个 -

if(bundle.getBoolean(CMWidget.HOME_BUTTON_CLICKED)) {
showQuickAction((ImageButton)findViewById(R.id.home_button));
}

这是实际带来弹出窗口的 showQuickAction() 函数 -

public void showQuickAction(View view){
//populate the quick action
quickAction = new QuickAction(view, SearchActivity.this);
final ImageButton homeButton = (ImageButton)findViewById(R.id.home_button);
final ActionItem quickActionEverything = new ActionItem();
quickActionEverything.setTitle("Everything");
quickActionEverything.setIcon(getResources().getDrawable(R.drawable.logo));
quickActionEverything.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
quickAction.dismiss();

CONTENT_TYPE = CONTENT_EVERYTHING;

homeButton.setImageResource(R.drawable.logo_arrow);
searchBox.setHint(R.string.search);

if(previousContentType != CONTENT_TYPE && queryString.length() != 0){
emptyListContents();
cancelRunningWSCall();
startSearch(queryString);
}
else if(queryString.length() != 0 )
populateList(CONTENT_TYPE);

else if(queryString.length() == 0)
showRecentSearches();

showSoftKeyboard();
previousContentType = CONTENT_TYPE;
}
});

final ActionItem quickActionContacts = getQuickActionItem(homeButton, CONTENT_CONTACTS, R.drawable.contacts, R.drawable.contacts_arrow);
final ActionItem quickActionMails = getQuickActionItem(homeButton, CONTENT_MAILS, R.drawable.mails, R.drawable.mails_arrow);
final ActionItem quickActionCalendars = getQuickActionItem(homeButton, CONTENT_CALENDARS, R.drawable.calendars, R.drawable.calendars_arrow);
final ActionItem quickActionDocuments = getQuickActionItem(homeButton, CONTENT_DOCUMENTS, R.drawable.docs, R.drawable.docs_arrow);


quickAction.addActionItem(quickActionEverything);
quickAction.addActionItem(quickActionMails);
quickAction.addActionItem(quickActionCalendars);
quickAction.addActionItem(quickActionDocuments);


quickAction.show();
}

这也是我的堆栈跟踪 -

STACK_TRACE=java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.android/com.test.android.SearchActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
at android.app.ActivityThread.access$2300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)


at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:509)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:828)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:740)
at com.test.android.QuickAction.show(QuickAction.java:68)
at com.test.android.SearchActivity.showQuickAction(SearchActivity.java:1541)
at com.test.android.SearchActivity.onCreate(SearchActivity.java:219)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
... 11 more

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:509)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:828)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:740)
at com.test.android.QuickAction.show(QuickAction.java:68)
at com.test.android.SearchActivity.showQuickAction(SearchActivity.java:1541)
at com.test.android.SearchActivity.onCreate(SearchActivity.java:219)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
at android.app.ActivityThread.access$2300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)


at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

编辑:我将 showQuickAction() 函数移至 onResume() 和 onStart()。运气不好 :(

这是另一个堆栈跟踪 -

java.lang.RuntimeException: Unable to resume activity {com.test.android/com.test.android.SearchActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3347)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3362)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)

at android.app.ActivityThread.access$2300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:509)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:828)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:740)
at com.test.android.QuickAction.show(QuickAction.java:68)
at com.test.android.SearchActivity.showQuickAction(SearchActivity.java:1546)
at com.test.android.SearchActivity.onResume(SearchActivity.java:262)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1255)
at android.app.Activity.performResume(Activity.java:3864)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3337)
... 12 more

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:509)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:828)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:740)
at com.test.android.QuickAction.show(QuickAction.java:68)
at com.test.android.SearchActivity.showQuickAction(SearchActivity.java:1546)
at com.test.android.SearchActivity.onResume(SearchActivity.java:262)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1255)
at android.app.Activity.performResume(Activity.java:3864)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3337)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3362)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)


at android.app.ActivityThread.access$2300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

它们通过 PendingIntent 启动一个正常的 Activity,并带有一个指示该 Activity 应显示此子菜单的操作。

单击该按钮时查看 logcat:

I/ActivityManager(  167): Starting: Intent { act=com.android.quicksearchbox.action.QSB_AND_SELECT_CORPUS flg=0x14200000 pkg=com.google.android.googlequicksearchbox cmp=com.google.android.googlequicksearchbox/.SearchActivity bnds=[14,86][86,169] (has extras) } from pid -1

与单击搜索字段时相反:

I/ActivityManager(  167): Starting: Intent { act=android.search.action.GLOBAL_SEARCH flg=0x14200000 pkg=com.google.android.googlequicksearchbox cmp=com.google.android.googlequicksearchbox/.SearchActivity bnds=[86,95][384,160] (has extras) } from pid -1

(act=.. 是随 Intent 发送的操作)


编辑以解决新的细节,请在此处查看评论:

我认为这里的问题是您尝试从 onCreate() 打开 Quickaction 对话框。你的 Activity 在那里准备好了,但它还不可见。如果您现在添加对话框,它会引用它的父 Activity 并指出它不可见。这给了你这个错误。尝试将 extra 的解析和快速操作对话框的创建移到 Activity 生命周期的后期。我想 onStart() 是最好的地方。

关于android - 如何创建像谷歌搜索小部件中的弹出菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8296801/

25 4 0