我正在创建一个类似于Android搜索小部件的小部件。
他们如何在点击过滤器时实施快速操作弹出窗口? (最左边带'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)
答案 0 :(得分:2)
他们通过PendingIntent
开始一个正常的活动,其中的操作表明活动应显示此子菜单。
单击该按钮时,请参阅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=..
是与意图一起发送的操作)
修改以添加新详细信息,请参阅此处的评论:
我认为这里的问题是您尝试从onCreate()
打开Quickaction对话框。你的活动在那里准备好了,但还不可见。如果您现在添加对话框,它会引用它的父活动,并注意到它不可见。哪个给你这个错误。尝试将extra的解析和quickaction对话框的创建移动到活动lifecyle中的稍后点。我想onStart()
是最好的选择。
答案 1 :(得分:0)
这是快速对话。看看这个example。但首先你应该开始一项活动