2016-04-03 102 views
2

,所以我目前工作的一个基本事件的组织者,它将不过我有问题的存储事件的惯例,并允许用户存储他们感兴趣的事件。问题添加Android的对话框

,如我想为每个按钮添加一个对话框,所以它会显示事件信息并给用户选项,如果他们想添加或不添加。

我与代码的问题是以下,这occours onclick。

android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不是一个应用程序

下面是一个在屏幕上显示的eventbutton本身的代码

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); 
      } 
     } 



     } 

任何帮助,将不胜感激感谢

(的输出目录下载,如果有人有兴趣或需要它)

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: [email protected] time:70384960 
04-03 18:26:32.137 4335-4335/com.example.daniel.myapplication I/Timeline: Timeline: Activity_idle id: [email protected] 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

什么问题你与你的应用程序有? –

+0

我一直得到这个错误 android.view.WindowManager $ BadTokenException:无法添加窗口 - 标记null不适用于应用程序 – daniel

+0

您能分享您的catlog吗? –

回答

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)); 
}