在 onCreate 之前系统服务对活动不可用? [英] System services not available to Activities before onCreate?

查看:25
本文介绍了在 onCreate 之前系统服务对活动不可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何解决这个错误.我对 Android 和 Java 非常熟悉,因此代码和解释将非常有帮助.有任何想法吗?谢谢.

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.

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) 中:

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

该类检查是否已安装 SD 卡 (StorageStateChecker.java):

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;
}
}

用于调用 Runnables 的类 (Set_AC_SortOrder.java):

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);
}
}

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

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;
}

}

推荐答案

我敢打赌,您正试图在 Constructor 中创建一个 CursorAdapter活动.

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

Context 在活动构造函数中不可用,它仅在 Activity.onCreate() 方法及其他方法中可用.

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

还有一个最重要的提示......

And for a top tip...

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

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.

这篇关于在 onCreate 之前系统服务对活动不可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆