System services not available to Activities before onCreate?

user742030 picture user742030 · May 6, 2011 · Viewed 16.6k times · Source

I cant figure out how to fix this error. I am very green to Android and Java so code will be very helpful along with explanations. Any Ideas? Thank-you.

The 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

In the 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();
    }
};

This class checks to see if the SD-Card is mounted (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;
}
}

The class for calling the 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);
}
}

My Adapter Class (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;
}

}

Answer

techiServices picture techiServices · May 6, 2011

I would bet that you are trying to create a CursorAdapter in the Constructor of your Activity.

Context is not available in the Activities Constructor, it is only available in the Activity.onCreate() method and beyond.

And for a top tip...

Create the CursorAdapter with a Cursor of null in Activity.onCreate() and use ListView.getAdapter().changeCursor(newCursor) to assign newCursor once a background thread has returned a populated Cursor.