所以我目前正在开发一个基本的活动组织者,它将存储一个约定的事件,并允许用户存储他们感兴趣的事件。
但是我遇到了问题,因为我想在每个按钮上添加一个对话框,这样它就会显示事件信息,如果他们想要添加或不给用户提供选项。
我对代码的问题如下,其中包括onclick。
android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不适用于应用程序
以下是屏幕上显示的事件按钮本身的代码
public class EventButton extends Button
{
public Button button = this;
public Event event;
public String eventHost;
public String eventName;
public String eventLocation;
public Calendar eventDate;
private LinearLayout screen;
public EventButton(Context context , Event pEvent){
super(context);
eventDate = pEvent.eventDate;
eventName = pEvent.eventName;
eventHost = pEvent.eventHost;
eventLocation = pEvent.eventLocation;
event = pEvent;
setOnClick();
this.setText(eventName + "\n " + eventHost);
}
public void setOnClick() {
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int timeTilEvent =getTimeDifference();
int day = eventDate.get(eventDate.DAY_OF_MONTH);
int month=eventDate.get(eventDate.MONTH);
int year=eventDate.get(eventDate.YEAR);
int hour=eventDate.get(eventDate.HOUR_OF_DAY);
int minute=eventDate.get(eventDate.MINUTE);
String minutestring=""+ minute;
if(minutestring.length()==1){
minutestring = "0"+minute;
}
String dateOutput= day + "/" + month +"/" + year + "\n " + hour + ":" + minutestring;
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage("Event:" + eventName + "\n Host:" + eventHost + "\n Location: " + eventLocation + "\n When: " + dateOutput);
builder.setTitle("do you wish to add this event to your watch list?");
if(!FileManager.eventList.contains(event)) {
builder.setPositiveButton("add interest", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
FileManager.writeToFile(new Event(eventHost, eventName, eventLocation, eventDate), getContext());
FileManager.readFromFile(getContext());
}
});
}
else if(FileManager.eventList.contains(event)) {
builder.setPositiveButton("remove interest", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
FileManager.eventList.remove(event);
FileManager.writeToFile(new Event(eventHost, eventName, eventLocation, eventDate), getContext());
FileManager.readFromFile(getContext());
}
});
}
builder.setPositiveButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
主类(显示按钮的创建方式)
public class MainActivity extends AppCompatActivity {
public Calendar setupDate(int year, int month, int day , int hour, int minute){
return new GregorianCalendar(year,month,day,hour,minute);
}
ArrayList<EventButton> buttons = new ArrayList<EventButton>();
public void addButtonToList(String pHost, String pName,String pEventLocation, Calendar pDate){
Event event = new Event(pHost, pName, pEventLocation, pDate);
buttons.add(new EventButton(getBaseContext(),event));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
addButtonToList("Host ", " Event", "location", setupDate(2016, 04, 03, 10, 00));
addButtonToList("Host ", " Event", "location", setupDate(2016, 04, 03, 10, 00));
LinearLayout ll = (LinearLayout) findViewById(R.id.LinearLayout1);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
for(int count=0;count < buttons.size();count++){
if(buttons.get(count) != null) {
buttons.get(count).AddButtonToScreen(ll, lp);
}
}
}
非常感谢任何帮助
(如果有人感兴趣或需要它,那么catlog输出)
04-03 18:26:20.267 4335-4335/? E/Zygote: v2
04-03 18:26:20.267 4335-4335/? I/libpersona: KNOX_SDCARD checking this for 10160
04-03 18:26:20.267 4335-4335/? I/libpersona: KNOX_SDCARD not a persona
04-03 18:26:20.267 4335-4335/? I/SELinux: Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-G361F_5.1.1_0043
04-03 18:26:20.267 4335-4335/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
04-03 18:26:20.277 4335-4335/? I/art: Late-enabling -Xcheck:jni
04-03 18:26:20.287 4335-4335/? D/TimaKeyStoreProvider: in addTimaSignatureService
04-03 18:26:20.307 4335-4335/? D/TimaKeyStoreProvider: TimaSignature is unavailable
04-03 18:26:20.307 4335-4335/? D/ActivityThread: Added TimaKesytore provider
04-03 18:26:20.307 4335-4335/? I/SAMP: ActivityThread() - SAMP_ENABLE : true
04-03 18:26:20.357 4335-4335/com.example.daniel.myapplication D/ContextImpl: ContextImpl running for user UserHandle{0} 0
04-03 18:26:20.367 4335-4335/com.example.daniel.myapplication D/ContextImpl: ContextImpl running for user UserHandle{0} 0
04-03 18:26:20.367 4335-4335/com.example.daniel.myapplication W/ResourcesManager: getTopLevelResources: null for user 0
04-03 18:26:20.437 4335-4335/com.example.daniel.myapplication D/ContextImpl: ContextImpl running for user UserHandle{0} 0
04-03 18:26:20.447 4335-4335/com.example.daniel.myapplication D/ContextImpl: ContextImpl running for user UserHandle{0} 0
04-03 18:26:20.447 4335-4335/com.example.daniel.myapplication W/ResourcesManager: getTopLevelResources: null for user 0
04-03 18:26:20.447 4335-4335/com.example.daniel.myapplication W/ResourcesManager: getTopLevelResources: null for user 0
04-03 18:26:20.457 4335-4335/com.example.daniel.myapplication D/DisplayManager: DisplayManager()
04-03 18:26:20.497 4335-4335/com.example.daniel.myapplication D/PhoneWindow: *FMB* installDecor mIsFloating : false
04-03 18:26:20.497 4335-4335/com.example.daniel.myapplication D/PhoneWindow: *FMB* installDecor flags : -2139029248
04-03 18:26:20.557 4335-4398/com.example.daniel.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-03 18:26:20.577 4335-4335/com.example.daniel.myapplication D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
04-03 18:26:20.577 4335-4335/com.example.daniel.myapplication D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
04-03 18:26:20.627 4335-4398/com.example.daniel.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
04-03 18:26:20.627 4335-4398/com.example.daniel.myapplication D/GC: <tid=4398> OES20 ===> GC Version : GC version rls_5011p6_GC5.5.14
04-03 18:26:20.637 4335-4398/com.example.daniel.myapplication D/OpenGLRenderer: Enabling debug mode 0
04-03 18:26:20.757 4335-4335/com.example.daniel.myapplication W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
04-03 18:26:20.757 4335-4335/com.example.daniel.myapplication I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@25ba99d time:70384960
04-03 18:26:32.137 4335-4335/com.example.daniel.myapplication I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@25ba99d time:70396347
04-03 18:26:33.527 4335-4335/com.example.daniel.myapplication D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
04-03 18:26:33.647 4335-4335/com.example.daniel.myapplication D/PhoneWindow: *FMB* installDecor mIsFloating : true
04-03 18:26:33.647 4335-4335/com.example.daniel.myapplication D/PhoneWindow: *FMB* installDecor flags : 8388610
04-03 18:26:33.687 4335-4335/com.example.daniel.myapplication D/AndroidRuntime: Shutting down VM
04-03 18:26:33.687 4335-4335/com.example.daniel.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.daniel.myapplication, PID: 4335
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:578)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:298)
at com.example.daniel.myapplication.EventButton$1.onClick(EventButton.java:195)
at android.view.View.performClick(View.java:5076)
at android.view.View$PerformClick.run(View.java:20279)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
答案 0 :(得分:1)
当您应该使用活动上下文时,您正在使用基本上下文。
不要使用getBaseContext()
,只需使用this
作为活动背景:
public void addButtonToList(String pHost, String pName,String pEventLocation, Calendar pDate){
Event event = new Event(pHost, pName, pEventLocation, pDate);
buttons.add(new EventButton(this, event));
}