Reputation: 65
so I am currently working on a basic event organiser, it will store events for a convention and allow the user to store the events they're interested in.
However I am having issues, as i want to add a dialog to each buttons press so it will display the events information and give the user the option if they want to add it or not.
The issue i am having with the code is the following, which occours onclick.
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Below is the code for the eventbutton itself which is displayed on screen
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();
Main class (showing how the buttons are created)
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);
}
}
}
Any help will be greatly appreciated thanks
(the catlog output if anyone was interested or needed it)
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)
Upvotes: 2
Views: 222
Reputation: 43342
You're using the Base Context when you should be using the Activity Context.
Instead of using getBaseContext()
, just use this
for the Activity Context:
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));
}
Upvotes: 1