android:调用setListAdapter

时间:2012-08-30 16:22:39

标签: android android-listview logcat simplecursoradapter

我的第一个来自activity2的setListAdapter正在运行,但如果我在activity1(tabhost)中搜索EditText来更新activity2中的setlistadapter(没有布局),我会收到错误

这是我的LogCat

08-30 16:03:36.119: D/AndroidRuntime(1482): Shutting down VM
08-30 16:03:36.119: W/dalvikvm(1482): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-30 16:03:36.157: E/AndroidRuntime(1482): FATAL EXCEPTION: main
08-30 16:03:36.157: E/AndroidRuntime(1482): java.lang.IllegalStateException: System services not available to Activities before onCreate()
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.app.Activity.getSystemService(Activity.java:3526)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:49)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:84)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at com.amnisar.pediatric.calculator.MedicineList.changeList(MedicineList.java:105)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at com.amnisar.pediatric.calculator.PediaHome$1.afterTextChanged(PediaHome.java:90)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.widget.TextView.sendAfterTextChanged(TextView.java:6145)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:6328)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.text.SpannableStringBuilder.sendTextHasChanged(SpannableStringBuilder.java:897)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:353)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:583)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:384)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:292)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:73)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.os.Looper.loop(Looper.java:123)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at java.lang.reflect.Method.invokeNative(Native Method)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at java.lang.reflect.Method.invoke(Method.java:521)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-30 16:03:36.157: E/AndroidRuntime(1482):     at dalvik.system.NativeStart.main(Native Method)
08-30 16:03:40.378: I/Process(1482): Sending signal. PID: 1482 SIG: 9

这是我在活动1中的代码(TabActivity)

etSearch = (EditText) findViewById(R.id.searchBox);

etSearch.addTextChangedListener(new TextWatcher(){

    public void afterTextChanged(Editable arg0) {

        database = entry.getWritableDatabase();
        EditText se = (EditText) findViewById(R.id.searchBox);
        String nametest= se.getText().toString().toUpperCase();
        Cursor cursor = database.query("medicineTable", new String[] {"_id", "Name"}, 
                "name LIKE " + "'%"+etSearch.getText().toString().toUpperCase()+"%'", null, null, null, null);
        cursor.moveToFirst();

        trythis.changeList(cursor);

    }

以下是我在活动2(listActivity)中的代码:

public void setList(){

    SQLite entry =  new SQLite(this);
    try{
        entry.createDataBase();
    }catch(IOException e){
        throw new Error("Error");
    }

    database = entry.getWritableDatabase();
    data = database.query("medicineTable", fields, null, null, null, null,
            null);



    dataSource = new SimpleCursorAdapter(this, R.layout.medlayout, data, fields,
            //new int[] { R.id.name,R.id.category,R.id.trade});
            new int[] { R.id.name});

    view = getListView();

    view.setHeaderDividersEnabled(true);
    view.addHeaderView(getLayoutInflater().inflate(R.layout.medlayout, null));

    setListAdapter(dataSource);
}

public void changeList(Cursor newData){

    dataSource = new SimpleCursorAdapter(this, R.layout.medlayout, newData, fields,
            new int[] { R.id.name});
    setListAdapter(dataSource);


}

1 个答案:

答案 0 :(得分:0)

正如Logcat所说,你试图在框架初始化之前调用其他Activity的方法。 我猜您已使用如下所示的新通话初始化Trythis。 您不能这样做,因为活动的上下文不会被初始化。 删除新电话

 trythis = new ListActivity();