Why am I getting an error when trying to use DisplayMetrics in Android?

I am just starting with Android programming and I get an error message in the following code:

DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); 

The first line perfectly matches Eclipse. But on the second line, this tells me that there is no return type getWindowManager. I do not understand this. When I search the Internet about how to use this code, everyone does the same. However, Eclipse gives me an error.

+4
source share
2 answers

If this code is used in the view instead of Activity, you need to do something like

 ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm); 

or

 ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)) .getDefaultDisplay().getMetrics(dm); 
+8
source

Providing more code will help people debug you.

From your OP,

But in the second line, it tells me that the return type "getWindowManager" is missing

getWindowManager is an Activity method, so make sure you have the code inside the action ( extends Activity ))

+1
source

Source: https://habr.com/ru/post/1337693/


All Articles