java.lang.IllegalStateException: System services not available to Activities before onCreate()这个错误说的还不够详细吗 意思是在构造函数(就是onCreate之前)里调用了一些Activity的方法,但是由于onCreate还没有执行过,所以System services 是无效的,而调用System services 是在Activity.java里android.app.Activity.getSystemService
构造方法是运行的,只是通常不这么用。 目测你的程序应该是第11行出的错 popuWindowAdapter = new ArrayAdapter<String>(this, R.layout.spinner_own_model, arrModeList); 在这一行中,你的this应该指的的是SecondActivity.this 而在此时,SecondActivity还没有执行过onCreat()方法,也就是报错信息所说的System services not available to Activities before onCreate()。所有建议你把那个this换成context,就是你在构造方法里面传进来的那个context对象(this.context)试试。
意思是在构造函数(就是onCreate之前)里调用了一些Activity的方法,但是由于onCreate还没有执行过,所以System services 是无效的,而调用System services 是在Activity.java里android.app.Activity.getSystemService
mode.showModeWindow();在SecondActivity中代码:
public SecondActivity(Context mContext) {
this.context = mContext;
initModeWindow();
}
private void initModeWindow() {
layoutInflater = LayoutInflater.from(context);
// LayoutInflater layoutInflater = (LayoutInflater) (this).getSystemService(LAYOUT_INFLATER_SERVICE);
View popView = layoutInflater.inflate(R.layout.short_popupwindow_layout, null);
lvModeWindow = (ListView) popView.findViewById(R.id.short_popuwindow_list_id);
arrModeList = context.getResources().getStringArray(R.array.screen_mode);
popuWindowAdapter = new ArrayAdapter<String>(this, R.layout.spinner_own_model, arrModeList);
popWindow = new PopupWindow(popView, POPUPWINDOW_WIDTH, LayoutParams.WRAP_CONTENT);
lvPopuWindow.setAdapter(popuWindowAdapter); popWindow.setOutsideTouchable(true);
popWindow.setFocusable(true);
popWindow.setBackgroundDrawable(new BitmapDrawable());
popWindow.update();
if(null != modeWindow) {
modeWindow.dismiss();
return;
}
}
public void showModeWindow(){
View homeView = layoutInflater.inflate(R.layout.home_layout, null);
popWindow.showAtLocation(homeView, Gravity.RIGHT | Gravity.TOP, POPUP_OFFSET_X, POPUP_OFFSET_Y);
}
protected void onCreate(Bundle savedInstanceState) {
//...
}
目测你的程序应该是第11行出的错
popuWindowAdapter = new ArrayAdapter<String>(this, R.layout.spinner_own_model, arrModeList);
在这一行中,你的this应该指的的是SecondActivity.this 而在此时,SecondActivity还没有执行过onCreat()方法,也就是报错信息所说的System services not available to Activities before onCreate()。所有建议你把那个this换成context,就是你在构造方法里面传进来的那个context对象(this.context)试试。
你重写了oncreate方法,里面却啥都没有,它当然报错了。目测把oncreate方法删掉可以解决。楼主你是怎么解决的啊?