无法使用选项卡启动活动错误

问题描述:

我正在尝试通过添加TabHost和一些选项卡来导航额外功能来扩展我的应用程序。目前的应用程序基本上搜索数据库。目前应用的工作流程:无法使用选项卡启动活动错误

  1. 应用程序加载到登录屏幕
  2. 用户登录
  3. 用户得到一个搜索表单和输入数据,按下“搜索”
  4. 搜索加载结果列表活动。 ..

使用新选项卡,有一个单独的选项卡用于搜索。我希望所有搜索活动都保留在该标签组内。所以,我创建了一个活动组来处理所有的这些:

public class searchGroup extends ActivityGroup {   
    public static searchGroup group; 
    private ArrayList<View> history; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      this.history = new ArrayList<View>(); 
      group = this;  
      View view = getLocalActivityManager().startActivity("search", new Intent(this,search.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 
      replaceView(view); 
     } 

    public void replaceView(View v) {    
     history.add(v);     
     setContentView(v); 
    } 

    public void back() { 
     if(history.size() > 0) { 
      history.remove(history.size()-1); 
      setContentView(history.get(history.size()-1)); 
     }else { 
      finish(); 
     } 
    } 

    @Override 
    public void onBackPressed() { 
     searchGroup.group.back(); 
     return; 
    } 
} 

在我的搜索活动的搜索按钮onClickListener:

view = searchGroup.group.getLocalActivityManager().startActivity("search_results",new Intent(search.this, search_results.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 
searchGroup.group.replaceView(view); 

这是我得到的崩溃:

02-11 13:43:49.481:E/AndroidRuntime(1165):java.lang.RuntimeException:无法启动活动 ComponentInfo {com.myApp/com.myApp.search_results}: android.view.WindowManager $ BadToke nException:无法添加窗口 - 令牌[email protected]是 无效;你的活动正在运行?

但是,如果我去掉从search_result活动的onCreate行:

new LoadSearches().execute(); 

没有崩溃,但我什么也没得到明显。 LoadSearches()是一个AsyncTask,负责向服务器发送消息并运行搜索字符串,然后将返回的数据填充到onPostExecute()中的ListActivity中。

我不太明白为什么它在这里崩溃,而不是通常当我切换活动。我应该如何解决这个问题?有没有更好的办法?我已经读了一些关于碎片的内容,但还没有做任何事情。

+0

我也收到这个错误你得到任何解决方案 – 2012-08-30 10:40:16

我已经决定,多拉出我的头发后,去与碎片。一些资源,我发现有用的转换我现有的应用程序使用的片段和标签:

Fragments in Android 2.2.1, 2.3, 2.0. Is this possible?

http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

我也有我的活动之间传递数据的问题。在使用intent/bundle的活动之间传递数据的方式实际上并不一样,但可以稍微修改并仍然有效。

旧的方式(从活动1将数据传递到活性2):

活性1

Intent myIntent = new Intent(search.this, search_results.class); 
Bundle b = new Bundle(); 
b.putString("SEARCHSTRING", strSearch); 
myIntent.putExtras(b); 
startActivityForResult(myIntent, 0); 

活性2

Bundle b = getIntent().getExtras(); 
strSearch = b.getString("SEARCHSTRING"); 

使用碎片,我不得不创建活性2的初始化:

public search_results newInstance(String strSearch){ 
    search_results f = new search_results(); 
    Bundle b = new Bundle(); 
    b.putString("SEARCHSTRING", strSearch); 
    f.setArguments(b);   
    return f;  
} 
使用此10

,新方法采用片段:

Avtivity1

Fragment newFragment = new search_results().newInstance(strSearch); 
FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.replace(R.id.realtabcontent, newFragment); 
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
ft.addToBackStack(null); 
ft.commit(); 

活性2(onCreateView)

Bundle b = getArguments(); 
strSearch = b.getString("SEARCHSTRING"); 

我希望这可以帮助别人,因为这是我很难找到所有这些信息在一个地方。