setContentView不允许搜索工作

时间:2012-03-30 16:31:31

标签: java android android-intent

自从采用TabHosts以来,我一直试图使我的搜索功能正常工作,我已经在我的onCreate方法中找到了我的Search类,当setContentView(R.layout.main);时它会导致以下错误

03-30 09:19:53.301: E/AndroidRuntime(728): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@4053c270 is not valid; is your activity running?

当我点击硬件搜索按钮弹出搜索对话框时我会将其注释掉,我可以输入一个值然后按搜索,然后我会得到下一个绊脚石

03-30 09:23:37.061: D/PhoneWindow(776): couldn't save which view has focus because the focused view com.android.internal.policy.impl.PhoneWindow$DecorView@4053ff38 has no id.

以下是我的搜索类

的代码
public class Search extends ListActivity {

private TextView mTextView;
//protected ListAdapter adapter;
private DxDbAdapter mDbHelper;
DxSimpleCursorAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

    //mTextView = (TextView) findViewById(R.id.text1);

    // Get the intent, verify the action and get the query
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      String query = intent.getStringExtra(SearchManager.QUERY);
      doMySearch(query);
    }
}

public void doMySearch(String query_results) {
    //SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();

    mDbHelper = new DxDbAdapter(this);
    mDbHelper.open();

    String[] columns = new String[] {"diagnosis", "diagcode"};
    int[] to = new int[] {R.id.diagnosis, R.id.code};

    //  Add "No Results Found" message 

    Cursor cursor = mDbHelper.search(query_results.trim());
    //Cursor cursor = db.rawQuery("Select _id, diagnosis, diagcode From DiagLookup Where diagnosis Like ? order by diagnosis asc", new String[]{"%"+query_results.trim()+"%"});

/*      if (cursor == null) {
        mTextView.setText(getString(R.string.no_results, new Object[] {query_results}));
    }
    else {*/ 
        adapter = new DxSimpleCursorAdapter(this,R.layout.list_detail,cursor,columns,to);
        setListAdapter(adapter);
 //     }
}

public void onListItemClick(ListView parent, View view, int position, long id) {
    SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
    Cursor c = (Cursor) getListAdapter().getItem(position);
    String arg = c.getString(c.getColumnIndex("_id"));
    Cursor cursor = db.rawQuery("Select category, subcategory From DiagLookup Where _id = ?",  new String[]{""+arg});
    cursor.moveToFirst();

    Context context = getApplicationContext();
    CharSequence text = "Category: " + cursor.getString(cursor.getColumnIndex("category")) + "\nSubcategory: " + cursor.getString(cursor.getColumnIndex("subcategory"));
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
}
}

此外,我创建了一个自定义SimpleCursorAdapter,因此我可以对imageview执行某些操作。当我最初输入adapter = new SimpleCursorAdapter(this,R.layout.list_detail,cursor,columns,to);输入搜索字词并按下搜索按钮时填充了列表视图但我的图像视图无效,因此当我将代码更改为adapter = new DxSimpleCursorAdapter(this,R.layout.list_detail,cursor,columns,to);并添加到顶部{{ 1}}它不再起作用并导致无法保存焦点错误。

我正在使用DxSimpleCursorAdapter adapter;TabActivity在我的应用顶部创建标签,我认为我的上下文搞砸了,导致焦点丢失......我不确定

对于如何解决这些问题有任何指导的人?

提前致谢。

1 个答案:

答案 0 :(得分:0)

  1. 如果您使用TabActivity,为什么还要再次使用ListActivity?如果要在选项卡中使用ListView,只需在布局xml文件中定义它。

  2. 如果您想在活动中使用标签,请尝试此操作,Android: TabHost without TabActivity

  3. 切换到ViewPager而不是TabActivity Set default page for ViewPager in Android