的onCreate之前不向活动的系统服务? [英] System services not available to Activities before onCreate?

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

问题描述

我无法弄清楚如何解决这个错误。我很绿色的Andr​​oid和Java这样code会随着解释非常有帮助。有任何想法吗?感谢你。

在LogCat中:

 致命异常:主要
ERROR / AndroidRuntime(13527):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.xxx.xxx/com.xxx.xxx.AC.List_AC}:java.lang.IllegalStateException:系统服务不提供给活动前的onCreate ()
ERROR / AndroidRuntime(13527):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
ERROR / AndroidRuntime(13527):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
ERROR / AndroidRuntime(13527):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
ERROR / AndroidRuntime(13527):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
ERROR / AndroidRuntime(13527):在android.os.Handler.dispatchMessage(Handler.java:99)
ERROR / AndroidRuntime(13527):在android.os.Looper.loop(Looper.java:123)
ERROR / AndroidRuntime(13527):在android.app.ActivityThread.main(ActivityThread.java:4627)
ERROR / AndroidRuntime(13527):在java.lang.reflect.Method.invokeNative(本机方法)
ERROR / AndroidRuntime(13527):在java.lang.reflect.Method.invoke(Method.java:521)
ERROR / AndroidRuntime(13527):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
ERROR / AndroidRuntime(13527):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
ERROR / AndroidRuntime(13527):在dalvik.system.NativeStart.main(本机方法)
ERROR / AndroidRuntime(13527):致:java.lang.IllegalStateException:之前的onCreate不向活动系统服务()
ERROR / AndroidRuntime(13527):在android.app.Activity.getSystemService(Activity.java:3526)
ERROR / AndroidRuntime(13527):在android.widget.ResourceCursorAdapter< INIT>(ResourceCursorAdapter.java:49)。
ERROR / AndroidRuntime(13527):在android.widget.SimpleCursorAdapter< INIT>(SimpleCursorAdapter.java:84)。
ERROR / AndroidRuntime(13527):在com.aeroTechnologies.flyDroid.AC.Adapter_AC< INIT>(Adapter_AC.java:21)。
ERROR / AndroidRuntime(13527):在com.xxx.xxx.AC.Set_AC_SortOrder.orderASC_Label(Set_AC_SortOrder.java:32)
ERROR / AndroidRuntime(13527):在com.xxx.xxx.AC.List_AC $ 1.运行(List_AC.java:49)
ERROR / AndroidRuntime(13527):在com.xxx.xxx.StorageStateChecker.performExternalStorageOperation(StorageStateChecker.java:10)
ERROR / AndroidRuntime(13527):在com.xxx.xxx.AC.List_AC.onCreate(List_AC.java:38)
ERROR / AndroidRuntime(13527):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
ERROR / AndroidRuntime(13527):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
ERROR / AndroidRuntime(13527):11 ...更多
 

在ListView的活动(List_AC.java):

 公共类List_AC扩展ListActivity {
/ **
 *  - 第一次创建活动时调用
 * ================================================= ==================
 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    的setContentView(R.layout.list_view2);

    activityTitle =(TextView中)findViewById(R.id.titleBarTitle);
    activityTitle.setText(顾问环行器);

    sea​​rchList();
    nextActivity();

    可运行doIfMounted = ORDER_ASC;
    StorageStateChecker.performExternalStorageOperation(doIfMounted);

}

/ **
 *  - 检查,看是否SD卡安装和放大器;加载默认列表顺序
 * ================================================= =====================
 ** /

私有静态最后的Runnable ORDER_ASC =新的Runnable(){
    公共无效的run(){
        Set_AC_SortOrder.orderASC_Label();
    }
};
 

本类检查是否Micro-SD卡安装(StorageStateChecker.java):

 公共类StorageStateChecker {

公共静态布尔performExternalStorageOperation(Runnable的doIfMounted){
    如果(android.os.Environment.getExternalStorageState()。等于(
            android.os.Environment.MEDIA_MOUNTED)){

        如果(doIfMounted!= NULL){
            doIfMounted.run();
        }
        返回true;

    }否则,如果(android.os.Environment.getExternalStorageState()。等于(
            android.os.Environment.MEDIA_UNMOUNTED)){
         //Alerts.sdCardMissing(this);
    }
    返回false;
}
}
 

本类调用的Runnable(Set_AC_SortOrder.java):

 公共类Set_AC_SortOrder {

私有静态最后上下文list_AC =新List_AC();
布尔mExternalStorageAvailable = FALSE;
布尔mExternalStorageWriteable = FALSE;
静态字符串extStorageDirectory = Environment.getExternalStorageDirectory()
        的ToString();
静态文件DBFILE =新的文件(extStorageDirectory
        +/Aero-Technologies/flyDroid/dB/flyDroid.db);
静态SQLiteDatabase DB = SQLiteDatabase.openOrCreateDatabase(DBFILE,NULL);

/ **
 *  - 默认列表顺序(标签/民升序)
 * ================================================= ====================
 ** /
公共静态无效orderASC_Label(){
    光标databaseCursor = db.rawQuery(
            SELECT * FROM AC_list ORDER BY`label` ASC,NULL);

    Adapter_AC databaseListAdapter =新Adapter_AC(list_AC,
            R.layout.list_item,databaseCursor,新的String [] {标签,
                    标题,说明,gotoURL},新的INT [] {
                    R.id.label,R.id.listTitle,R.id.caption,R.id.dummy});

    databaseListAdapter.notifyDataSetChanged();
    ((ListActivity)list_AC).setListAdapter(databaseListAdapter);
}
}
 

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

 公共类Adapter_AC扩展SimpleCursorAdapter {


静态游标dataCursor;
私人LayoutInflater mInflater;

公共Adapter_AC(上下文的背景下,INT布局,光标dataCursor,
        的String []从,INT []键){
    超(背景下,布局,dataCursor,从,到);
    this.dataCursor = dataCursor;
    mInflater = LayoutInflater.from(上下文);
}

公共查看getView(INT位置,查看convertView,ViewGroup中父){

    ViewHolder持有人;

    如果(convertView == NULL){
        convertView = mInflater.inflate(R.layout.list_item,NULL);

        持有人=新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(保持器);

    } 其他 {
        支架=(ViewHolder)convertView.getTag();
    }

    dataCursor.moveToPosition(位置);

    INT label_index = dataCursor.getColumnIndex(标签);
    字符串标签= dataCursor.getString(label_index);

    INT title_index = dataCursor.getColumnIndex(标题);
    字符串标题= dataCursor.getString(title_index);

    INT description_index = dataCursor.getColumnIndex(说明);
    字符串描述= dataCursor.getString(description_index);

    INT goto_index = dataCursor.getColumnIndex(gotoURL);
    字符串gotoURL = dataCursor.getString(goto_index);

    holder.text1.setText(标签);
    holder.text1.setTag(标签);
    holder.text2.setText(职称);
    holder.text3.setText(介绍);
    //holder.text4.setText(gotoURL);
    holder.text4.setTag(gotoURL);

    返回convertView;
}

静态类ViewHolder {
    TextView的文本1;
    TextView的文本2;
    TextView的文本3;
    TextView的文本4;
}

}
 

解决方案

我敢打赌,你要创建一个的CursorAdapter 构造函数活动的

上下文不在活动构造可用的,它只是在 Activity.onCreate()方法可用和超越。

和一个顶端尖...

创建的CursorAdapter 与空的 Activity.onCreate一个光标()和使用 ListView.getAdapter()。changeCursor(newCursor)指定 newCursor 一次背景线程返回一个人口稠密光标

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

}

解决方案

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.

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

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