2012-03-30 42 views
0

我一直tryng让我的搜索功能,因为采用TabHosts曾经工作,我已经想通了,我为我的搜索类onCreate方法,当setContentView(R.layout.main);它导致以下错误的setContentView不允许搜索工作

03-30 09:19:53.301: E/AndroidRuntime(728): android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] 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 [email protected] 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);输入一个搜索词并按下搜索按钮时已经填充了listview,但我的imageview没有运行,所以当我刚刚将代码更改为adapter = new DxSimpleCursorAdapter(this,R.layout.list_detail,cursor,columns,to);并添加到顶部DxSimpleCursorAdapter adapter;时,它不再起作用, t保存焦点错误。

我正在使用TabActivityActivityGroup沿我的应用程序的顶部创建选项卡,我认为我的上下文被搞砸了,这导致焦点丢失...我不确定。

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

在此先感谢。

回答

0
  1. 如果您使用TabActivity,为什么再次使用ListActivity?如果你想在Tabs中使用ListView,你可以简单地在layout xml文件中定义它。

  2. 如果你想使用一个活动的卡口,然后尝试了这一点,Android: TabHost without TabActivity

  3. 切换到ViewPager代替TabActivity Set default page for ViewPager in Android

+0

提供更多背景的TabActivity实施等我遵循[这里]概述的例子(http://ericharlow.blogspot.ca/2010/09/experience-multiple-android-activities.html) – DeucePie 2012-03-30 16:46:04