2016-04-03 58 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)); 
}