2011-07-01 36 views
0

我面臨的問題是如何通過基於Button單擊android的嵌套 活動導航選項卡活動。基於Button的多個嵌套活動的TabActivity單擊

我有3個選項卡儀表板,車輛搜索和位置搜索。當我 按位置搜索選項卡我得到一個編輯文本(輸入郵政編碼) 和去按鈕(當我按下它時,我應該得到位於不同頁面上的郵政編碼100英里 稱爲位置搜索結果頁)

我的具體問題是應用程序崩潰,當我按下按鈕去和 之前,我的位置

我有延伸的TabActivity MainActivity類別和定義所有 標籤

public class MainActivity extends TabActivity 
{ 
    public TabHost tabHost; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     tabHost = (TabHost) findViewById(android.R.id.tabhost); 
     TabHost.TabSpec spec; 
     Intent intent; 

     intent = new Intent().setClass(this, DashBoard.class); 
     spec = 
tabHost.newTabSpec("dashboard").setIndicator("DashBoard").setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, VehicleSearch.class); 
     spec = 
tabHost.newTabSpec("vehicleSearch").setIndicator("VehicleSearch").setContent(intent); 
     tabHost.addTab(spec); 

     intent = new Intent().setClass(this, BranchSearch.class); 
     spec = 
tabHost.newTabSpec("branchSearch").setIndicator("BranchSearch").setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.setCurrentTab(3); 


} 

我也有延伸的ActivityGroup

public class BranchSearchHelper extends ActivityGroup 
{ 
    public static BranchSearchHelper branchSearch; 
    private ArrayList<View> history; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     branchSearch = this; 
     this.history = new ArrayList<View>(); 


     View view = 
getLocalActivityManager().startActivity("BranchSearch", new 
Intent(this,BranchSearch.class) 
       .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 

     replaceView(view); 

    } 

    public void replaceView(View v) 
    { 
          // Adds the old one to history 
        history.add(v); 
          // Changes this Groups View to the new 
View. 
        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() 
       { 

       BranchSearchHelper.branchSearch.back(); 
        return; 
       } 


} 

類BranchSearch擴展活動的BranchSearchHelper類

public class BranchSearch extends Activity implements OnClickListener 
{ 

    public void onCreate(Bundle savedInstanceState) 
    { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.branchsearch); 
      Button locSearch = (Button) 
findViewById(R.id.btnlocSearch); 
      locSearch.setOnClickListener(this); 


     } 

     public void onClick(View v) 
    { 
       // TODO Auto-generated method stub 

       EditText editText = (EditText) 
findViewById(R.id.lsearch); 

       Bundle bundle = new Bundle(); 
       bundle.putString("zipCode", 
editText.getText().toString()); 

       Intent i = new Intent(getApplicationContext(), 
LocationSearchResults.class); 
       i.putExtras(bundle); 
       i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 


       View view = 
BranchSearchHelper.branchSearch.getLocalActivityManager().startActivity("Locations 
Results",i).getDecorView(); 

       BranchSearchHelper.branchSearch.replaceView(view); 




      } 


} 

我總是得到一個javaNUllPointerexception異常拋出在

View view = 
BranchSearchHelper.branchSearch.getLocalActivityManager().startActivity("Locations 
Results",i).getDecorView(); 

因爲branchSearch爲空

所以你能告訴我我怎麼樣可以保留製表符的軌跡,並在按下執行按鈕而沒有 使應用程序崩潰時顯示所有位置結果。 (我應該換什麼樣的代碼部分)

有一個叫LocationSearchResults類處理 顯示所有位置搜索結果

回答

0

branchSearch = this;將使branchSearch一個的ActivityGroup。爲了解決這個問題,你可以刪除它並在onClick聲明的底部添加branchSearch = view;行。

+0

它沒有工作任何人都可以給我另一個建議 – Youssef