在onCreate()之前,活动无法使用Android系统服务

时间:2013-02-12 16:17:48

标签: android runtime-error

....    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

done = true;
}

...


public class addcontacts extends ListActivity {

     protected void onCreate() {

     }
     protected void execute(final Boolean success) {
if (done) {
         adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
         setListAdapter(adapter);
}
     }

 }


public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {

@Override
    protected Boolean doInBackground(Void... params) {

...

}
 @Override
    protected void onPostExecute(final Boolean success) {

            setContentView(R.layout.main);


            ac = new addcontacts();
            ac.execute(true);


    }

...

public class getcontacts extends AsyncTask<Void, Void, Boolean> {
    @Override
    protected Boolean doInBackground(Void... params) {


...
listItems.add(json_data.getString("login") + " " + derp);
...
}

...
}

 @Override
    protected void onPostExecute(final Boolean success) {
        getc = null;
        //showProgress(false);
        adapter.notifyDataSetChanged();
}

运行时错误:

 02-12 15:59:35.274: E/AndroidRuntime(1308): FATAL EXCEPTION: main
 02-12 15:59:35.274: E/AndroidRuntime(1308): java.lang.IllegalStateException: System services not available to Activities before onCreate()
 02-12 15:59:35.274: E/AndroidRuntime(1308):    at android.app.Activity.getSystemService(Activity.java:3989)

 02-12 15:59:35.274: E/AndroidRuntime(1308):    at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
 02-12 15:59:35.274: E/AndroidRuntime(1308):    at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)

任何想法如何解决?

1 个答案:

答案 0 :(得分:2)

唯一被打破的是你的代码。修复将是在onCreate()完成之前避免访问系统服务,否则尚未对活动对象进行任何设置,因此您面临自我解释的原因异常。