我正在创建一个类似于Android的Google搜索部件的部件。如何在Google搜索小部件中创建弹出式菜单?
过得好他们实现上点击过滤器的行动迅速弹出?
这里(用“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)
好吧,我做了同样的事情。我使用Pending Intent开始了一项活动。在活动中,我调用了一个显示弹出窗口的函数。但是,我的应用程序崩溃了Bad Token异常 - “您的活动正在运行吗?”信息。 后来我用了一个线程,延迟了0.5s,然后调用了相同的函数。有用。这延迟的事情有点令人毛骨悚然。有没有其他方法可以解决这个问题? – Enigma
很难说没有看到代码是诚实的。听起来像某些东西是关闭的,但不能说出什么。对我来说似乎不正常。如果你真的想知道/修复,我会说打开一个新的问题,其中包括与解释和堆栈跟踪的代码。 – 2011-11-28 14:09:24
我用代码更新了这个问题。 – Enigma