android - onCreate 之前的 Activity 无法使用系统服务?

标签 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 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):

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

}

最佳答案

我敢打赌,您正在尝试在 ActivityConstructor 中创建一个 CursorAdapter

Context 在 Activity Constructor 中不可用,它仅在 Activity.onCreate() 方法及以后可用。

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

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

关于android - onCreate 之前的 Activity 无法使用系统服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5905587/

相关文章:

java - 同时通过 2 个不同的声卡播放 2 首音乐

java - 闹钟不会停止响 - 取消按钮不起作用

c# - 你是把你的计算放在你的 set 上还是 gets 上?

java - Selenium - 不一致的 StaleElementReference 错误

java - 在 onViewCreated() 中选择复选框的问题

android - 在 HTC Sense 中显示同步联系人

java - 通用传感器名称

Python 类和单元测试

python - 括号对方法和 'Self' 变量的影响

ListView中的Android自定义列表项