系统服务不可用于onCreate之前的活动?

我无法弄清楚如何解决这个错误。 我对Android和Java非常绿色,所以代码会随着解释而变得非常有帮助。 有任何想法吗? 谢谢。

LogCat:

FATAL EXCEPTION: main ERROR/AndroidRuntime(13527): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.xxx/com.xxx.xxx.AC.List_AC}: java.lang.IllegalStateException: System services not available to Activities before onCreate() ERROR/AndroidRuntime(13527): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) ERROR/AndroidRuntime(13527): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) ERROR/AndroidRuntime(13527): at android.app.ActivityThread.access$2300(ActivityThread.java:125) ERROR/AndroidRuntime(13527): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) ERROR/AndroidRuntime(13527): at android.os.Handler.dispatchMessage(Handler.java:99) ERROR/AndroidRuntime(13527): at android.os.Looper.loop(Looper.java:123) ERROR/AndroidRuntime(13527): at android.app.ActivityThread.main(ActivityThread.java:4627) ERROR/AndroidRuntime(13527): at java.lang.reflect.Method.invokeNative(Native Method) ERROR/AndroidRuntime(13527): at java.lang.reflect.Method.invoke(Method.java:521) ERROR/AndroidRuntime(13527): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) ERROR/AndroidRuntime(13527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) ERROR/AndroidRuntime(13527): at dalvik.system.NativeStart.main(Native Method) ERROR/AndroidRuntime(13527): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate() ERROR/AndroidRuntime(13527): at android.app.Activity.getSystemService(Activity.java:3526) ERROR/AndroidRuntime(13527): at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:49) ERROR/AndroidRuntime(13527): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:84) ERROR/AndroidRuntime(13527): at com.aeroTechnologies.flyDroid.AC.Adapter_AC.<init>(Adapter_AC.java:21) ERROR/AndroidRuntime(13527): at com.xxx.xxx.AC.Set_AC_SortOrder.orderASC_Label(Set_AC_SortOrder.java:32) ERROR/AndroidRuntime(13527): at com.xxx.xxx.AC.List_AC$1.run(List_AC.java:49) ERROR/AndroidRuntime(13527): at com.xxx.xxx.StorageStateChecker.performExternalStorageOperation(StorageStateChecker.java:10) ERROR/AndroidRuntime(13527): at com.xxx.xxx.AC.List_AC.onCreate(List_AC.java:38) ERROR/AndroidRuntime(13527): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) ERROR/AndroidRuntime(13527): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) ERROR/AndroidRuntime(13527): ... 11 more 

在ListView Activity(List_AC.java)中:

 public class List_AC extends ListActivity { /** * -- Called when the activity is first created * =================================================================== */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.list_view2); activityTitle = (TextView) findViewById(R.id.titleBarTitle); activityTitle.setText("ADVISORY CIRCULATORS"); searchList(); nextActivity(); Runnable doIfMounted = ORDER_ASC; StorageStateChecker.performExternalStorageOperation(doIfMounted ); } /** * -- Check to See if the SD Card is Mounted & Loads Default List Order * ====================================================================== **/ private static final Runnable ORDER_ASC = new Runnable() { public void run() { Set_AC_SortOrder.orderASC_Label(); } }; 

这个类检查SD卡是否被挂载(StorageStateChecker.java):

 public class StorageStateChecker { public static boolean performExternalStorageOperation(Runnable doIfMounted) { if (android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED)) { if (doIfMounted != null) { doIfMounted.run(); } return true; } else if (android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_UNMOUNTED)) { //Alerts.sdCardMissing(this); } return false; } } 

调用Runnables(Set_AC_SortOrder.java)的类:

 public class Set_AC_SortOrder { private static final Context list_AC = new List_AC(); boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; static String extStorageDirectory = Environment.getExternalStorageDirectory() .toString(); static File dbfile = new File(extStorageDirectory + "/Aero-Technologies/flyDroid/dB/flyDroid.db"); static SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null); /** * -- Default List Order ( Label/Num Ascending) * ===================================================================== **/ public static void orderASC_Label() { Cursor databaseCursor = db.rawQuery( "SELECT * FROM AC_list ORDER BY `label` ASC", null); Adapter_AC databaseListAdapter = new Adapter_AC(list_AC, R.layout.list_item, databaseCursor, new String[] { "label", "title", "description", "gotoURL" }, new int[] { R.id.label, R.id.listTitle, R.id.caption, R.id.dummy }); databaseListAdapter.notifyDataSetChanged(); ((ListActivity) list_AC).setListAdapter(databaseListAdapter); } } 

我的适配器类(Adapter_AC.java):

 public class Adapter_AC extends SimpleCursorAdapter { static Cursor dataCursor; private LayoutInflater mInflater; public Adapter_AC(Context context, int layout, Cursor dataCursor, String[] from, int[] to) { super(context, layout, dataCursor, from, to); this.dataCursor = dataCursor; mInflater = LayoutInflater.from(context); } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.list_item, null); holder = new ViewHolder(); holder.text1 = (TextView) convertView.findViewById(R.id.label); holder.text2 = (TextView) convertView.findViewById(R.id.listTitle); holder.text3 = (TextView) convertView.findViewById(R.id.caption); holder.text4 = (TextView) convertView.findViewById(R.id.dummy); holder.text4.setVisibility(View.GONE); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } dataCursor.moveToPosition(position); int label_index = dataCursor.getColumnIndex("label"); String label = dataCursor.getString(label_index); int title_index = dataCursor.getColumnIndex("title"); String title = dataCursor.getString(title_index); int description_index = dataCursor.getColumnIndex("description"); String description = dataCursor.getString(description_index); int goto_index = dataCursor.getColumnIndex("gotoURL"); String gotoURL = dataCursor.getString(goto_index); holder.text1.setText(label); holder.text1.setTag(label); holder.text2.setText(title); holder.text3.setText(description); //holder.text4.setText(gotoURL); holder.text4.setTag(gotoURL); return convertView; } static class ViewHolder { TextView text1; TextView text2; TextView text3; TextView text4; } } 

Solutions Collecting From Web of "系统服务不可用于onCreate之前的活动?"

我敢打赌,你正试图在你的ActivityConstructor中创build一个CursorAdapter

Context在“活动构造器”中不可用,仅在Activity.onCreate()方法中可用。

而对于顶尖的技巧

Activity.onCreate()中用Cursor为null创buildCursorAdapter ,并使用ListView.getAdapter().changeCursor(newCursor)在后台线程返回填充的Cursor分配newCursor