转载

android – 如何使用另一个java文件的getWindowManager()

我想在我的MainActivity中使用getWindowManager(),但我不想直接在其中编写此方法.

这是我的MainActivity代码

public class MainActivity extends Activity {
@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  String d= new DisplayMessageActivity().getWeithAndHeight(d);
 }

这是我的DisplayMessageActivity代码

public class DisplayMessageActivity extends Activity {

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    ....
    ....
    setContentView(textView);
}
public String getWeithAndHeight(String url) {  
    DisplayMetrics dm = new DisplayMetrics();  
    *getWindowManager().getDefaultDisplay().getMetrics(dm);*
    int width = dm.widthPixels;
    int height = dm.heightPixels;  
    String w=new String(""+width);
    String h=new String(""+height);
    url=url+"&23=w%3A"+w+"%20h%3A"+h+"%20d%3A"; 
    return url;
}  
}

我的代码在getWindowManager()中断,请告诉我原因.非常感谢你.

public String getWeithAndHeight(Context context, String url) {  
    DisplayMetrics dm = new DisplayMetrics();  
    ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
    //....
}

>

getWindowManager() is a method of Activity class

翻译自:https://stackoverflow.com/questions/18225331/how-do-i-use-getwindowmanager-by-another-java-file

原文  https://codeday.me/bug/20190111/519734.html
正文到此结束
Loading...