2016-07-22 3 views
0

私は、アプリケーションでカスタム通知を使用してwindowmanagerをaddviewします。

android.view.WindowManager $ BadTokenException:ウィンドウトークンを追加できません。[email protected]は無効です。あなたの活動は実行されていますか? android.view.WindowManagerImpl.addViewでandroid.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:319) でandroid.view.ViewRootImpl.setView(ViewRootImpl.java:685) (WindowManagerImpl.java:85) ででcom.zuck3.petpolar.manager.InboxNotificationManager.showInboxNotification(InboxNotificationManager.java:82) (com.zuck3.petpolar.activity.Inbox.InboxActivity.onCreate(InboxActivity.java:66) android.app.Activity.performCreate Activity.java:6376)android.app.ActivityThread.handleLaunchActivityでandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519) でandroid.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113) で(ActivityThread。 java:2654)android.os.Handler.dispatchMessage(Handler.java:111) (英語)ハンドブック: android.os.Looper.loop(Looper.java:207) W/System.err:android.app.ActivityThread.main(ActivityThread.java:5728) W/System.err:java.lang.reflectにあります。 .Method.invoke(ネイティブメソッド) W/System.err:com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) W/System.err:com.android.internal。 os.ZygoteInit.main(ZygoteInit.java:679) W/libEGL:[ANDROID_RECORDABLE]形式:1

InboxNotificationManager.java

public class InboxNotificationManager { 
private static InboxNotificationManager instance; 
private WindowManager mWindowManager; 
private WindowManager.LayoutParams mWindowParams; 
private InboxNotificationView notificationView; 
private Runnable runnable; 
private Handler handler = new Handler(Looper.getMainLooper()); 
private Animation.AnimationListener animationListener = new Animation.AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { 

    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     hideInboxNotification(); 

    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 

    } 
}; 


public static InboxNotificationManager Instance(Context context) { 
    if (instance == null) { 
     instance = new InboxNotificationManager(context); 
    } 
    return instance; 

} 

public InboxNotificationManager(Context context) { 
    notificationView = new InboxNotificationView(context); 
    mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
    mWindowParams = new WindowManager.LayoutParams(); 
    mWindowParams.gravity = Gravity.TOP | Gravity.RIGHT; 
    mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT; 
    mWindowParams.width = WindowManager.LayoutParams.MATCH_PARENT; 
    mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 
      | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; 
    mWindowParams.format = PixelFormat.TRANSLUCENT; 
    runnable = new Runnable() { 
     @Override 
     public void run() { 
      notificationView.stopAnimation(animationListener); 

     } 
    }; 


} 

public void setTextMeesage(String meesage) { 
    notificationView.setTextMessage(meesage); 
    // Toast.makeText(InboxNotificationManager.this, "", Toast.LENGTH_SHORT).show(); 


} 

public void showInboxNotification(String message) { 
    if (notificationView.getWindowToken() == null) { 
     mWindowManager.addView(notificationView, mWindowParams); 
     notificationView.startAnimation(); 
     setTextMeesage(message); 

    } else { 
     // mWindowManager.setText(count); 

    } 
    notificationView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      notificationView.stopAnimation(animationListener); 

     } 
    }); 

    handler.removeCallbacks(runnable); 
    handler.postDelayed(runnable, 3000); 


} 

public void hideInboxNotification() { 
    if (notificationView.getWindowToken() != null) { 
     mWindowManager.removeView(notificationView); 
    } 


} 

そして、私はこのクラスを使用

InboxNotificationManager.Instance(BaseActivity.this).showInboxNotification("Hello world"); 

ありがとうとごめん、私の悪い英語。あなたはここにSO上でこのエラーに関連付けられている他の回答を

+0

をチェックする

if(mActivity != null && !mActivity.isFinishing()) 

を使用し、あなたの活動が実行されていることを確認をチェックアウトしましたか?私は2-3を見つけた。 'onCreate'メソッド全体を共有してください。 – Vucko

+0

アクティビティが終了し、その時点でビューが追加されると、この例外がスローされます。したがって、ビュー/ダイアログを表示する前に、アクティビティで 'if(!isFinishing())'をチェックしてください。 –

+0

ありがとうございます。しかし、私はlikeToastしたいすべての活動を私のカスタムビューを表示したい。 –

答えて

関連する問題