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

android– 在onCreate之前,活动无法使用系统服务?,第1张

概述我无法弄清楚如何解决此错误.我对Android和Java非常环保,因此代码将非常有用以及解释.有任何想法吗?谢谢.LogCat:FATALEXCEPTION:mainERROR/AndroidRuntime(13527):java.lang.RuntimeException:UnabletostartactivityComponentInfo{com.xxx.xxx/com.xxx.xxx.AC.List_AC}:

我无法弄清楚如何解决此错误.我对Android和Java非常环保,因此代码将非常有用以及解释.有任何想法吗?谢谢.

LogCat:

FATAL EXCEPTION: mainERROR/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.access00(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.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 * =================================================================== */@OverrIDepublic 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;}}

解决方法:

我敢打赌,你正在尝试在Activity的构造函数中创建一个CursorAdapter.

活动构造函数中不提供上下文,它仅在Activity.onCreate()方法及更高版本中可用.

最重要的是……

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

总结

以上是内存溢出为你收集整理的android – 在onCreate之前,活动无法使用系统服务?全部内容,希望文章能够帮你解决android – 在onCreate之前,活动无法使用系统服务?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1102365.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存