具有基于Button Click的多个嵌套活动的TabActivity

时间:2011-07-01 21:15:40

标签: android

我面临的问题是如何使用嵌套来浏览制表符活动 基于Button的活动点击Android。

我有3个选项卡仪表板,车辆搜索和位置搜索。当我 按位置搜索选项卡我得到一个编辑文本(输入邮政编码) 并按下按钮(当我按下它时,我应该获得100英里的位置 在不同页面中的邮政编码称为位置搜索结果页面)

我的具体问题是当我按下go按钮时应用程序崩溃了 在我获得位置之前

我有MainActivity类,它扩展了TabActivity并定义了所有 标签

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);
}

我还有BranchSearchHelper类,它扩展了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类扩展了Activity

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为空

所以,请你告诉我如何保持标签和节目的轨道 没有按下go按钮的所有位置结果 崩溃的应用程序。 (我应该改变代码的哪些部分)

有一个叫做LocationSearchResults的类来处理 显示所有位置搜索结果

1 个答案:

答案 0 :(得分:0)

branchSearch = this;将使branchSearch成为一个ActivityGroup。要解决此问题,您可以删除它并在onClick声明的底部添加行branchSearch = view;