在onCreate之前,活动无法使用系统服务?

时间:2011-05-06 00:28:30

标签: android class listview listadapter

我无法弄清楚如何修复此错误。我对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活动(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;
}

}

1 个答案:

答案 0 :(得分:5)

我敢打赌,您正尝试在CursorAdapter的{​​{1}}中创建Constructor

Activity在“活动构造函数”中不可用,它仅在Context方法及更高版本中可用。

最重要的提示......

Activity.onCreate()中创建CursorAdapterCursor的{​​{1}},并在后台线程返回填充的{{1}时使用Activity.onCreate()分配ListView.getAdapter().changeCursor(newCursor) }}