0

My code:


    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NoticeBoard.init(MainActivity.this);
        NoticeBoard.getInstance().setButton();
        setContentView(NoticeBoard.getInstance());
    }

}

    public class NoticeBoard extends ImageView {
    private static NoticeBoard instance = null;
    private static Paint paint;
    protected  Context mContext;
    private static WindowManager mWindowManager;

    public NoticeBoard(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public static NoticeBoard getInstance() {
        return instance;
    }

    public static void init(Context context) {
        instance = new NoticeBoard(context);
        instance.setImageResource(R.drawable.ic_launcher);
        instance.invalidate();
    }

    public NoticeBoard(Context context) {
        super(context);
        mWindowManager = (WindowManager) getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE); 
        mContext = context;
        setBackgroundColor(Color.BLACK);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint = new Paint(Paint.LINEAR_TEXT_FLAG);
        paint.setColor(Color.GRAY);
        paint.setTextSize(12.0F);
        canvas.drawText("Hello World", 100, 200, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    public void setButton() {
        Activity a =(Activity) mContext;
        ImageButton btn = new ImageButton(a);
        btn.setBackgroundResource(R.drawable.ic_launcher);
        android.view.WindowManager.LayoutParams paramsSplit = new android.view.WindowManager.LayoutParams();
        paramsSplit.width=2;
        paramsSplit.height=2;
        instance.mWindowManager.addView(btn, paramsSplit);
    }
}

I tried to find the answer, I changed getApplicationContext() to MainActivity.this, and changed context to (Activity)Context, but it still is not working. What's wrong with it? Ps.I'm Chinese, and my english is not very good. Thanks for your help.

mattfred
  • 2,689
  • 1
  • 22
  • 38
Jhon Smith
  • 181
  • 2
  • 12
  • @EJK I changed getApplicationContext() to MainActivity.this,but still not work. – Jhon Smith Jun 17 '15 at 02:50
  • This is just a guess, but how about changing " mWindowManager = (WindowManager) getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE); " to "mWindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); " – EJK Jun 17 '15 at 02:55
  • To clarify my above comment: simply try removing the "getApplicationContext()" portion of that line. – EJK Jun 17 '15 at 02:56

1 Answers1

0

On the following line:

    mWindowManager = (WindowManager) getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE); 

remove the reference to the application context

    mWindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); 

The context of the activity will be sufficient.

EJK
  • 12,332
  • 3
  • 38
  • 55