TabActivity with multiple nested activities based on Button Click
The problem I face is how to navigate through tab activity with nested activities based on Button click Android.
I have 3 tabs Dashboard, Vehicle search, and Location search. When I press the location search tab I get a Edit text(to enter the zip code) and go button(when I press it I should get the locations in 100 miles of zip code in different page called Location Search Results page)
My specific problem is the app crashes when I press go button and before I get the locations
I have MainActivity class which extends the TabActivity and define all the tabs
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);
}
I also have the BranchSearchHelper class which extends 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;
}
}
The class BranchSearch extends 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);
}
}
I always get an javaNUllPoint开发者_如何学运维erexception the exception throws at
View view =
BranchSearchHelper.branchSearch.getLocalActivityManager().startActivity("Locations
Results",i).getDecorView();
because branchSearch is null
so can you please tell me how I can keep the track of tabs and show the all the location results when I press the go button without crashing the application. (what parts of code should I change)
There is a class called LocationSearchResults which handles the displaying of all location search results
The line branchSearch = this;
will make branchSearch an ActivityGroup. To rectify this you could remove this and add the line branchSearch = view;
at the bottom of the onClick declaration.
精彩评论