问题
I am building an app which looks something like this.
It has a action bar and Tabs as shown below.
Tab 1: Should display images with ViewPage and indicator.
Tab 2: Should display maps.
I am new to android, but still somehow managed to build an app using internet resources and with the help of samples source. But i got stuck near mapview (when click on Tab2). I used TabHost, but this dint work, later on came to know that google has released new API supporting Map fragments. I even used Google API for MapFragment(the new one) but what i ended up with was some junk code. I messed up everything. I wanted to start again from scratch. Can any one brief about how implementing it.?
MapFragment.java
public class MapsFragment extends Fragment {
MapView map;
LayoutInflater inflater_;
ViewGroup container_;
View layout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
inflater_=inflater;
container_=container;
super.onCreate(savedInstanceState);
layout = inflater.inflate(R.layout.fragment_maps, container, false);
map = (MapView) layout.findViewById(R.id.mapView);
return (LinearLayout) layout;
}
PlaceACtivity.java
public class PlaceActivity extends SherlockFragmentActivity implements
OnShareTargetSelectedListener {
FragmentManager fm = getSupportFragmentManager();
DetailsFragment fragment_det = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tabMaps = actionBar.newTab();
ActionBar.Tab tabDetails = actionBar.newTab();
tabDetails.setText("Details");
tabMaps.setText("Maps");
tabDetails.setTabListener(new MyTabListener());
tabMaps.setTabListener(new MyTabListener());
actionBar.addTab(tabDetails);
actionBar.addTab(tabMaps);
}
private class MyTabListener implements ActionBar.TabListener {
@Override
public void onTabSelected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
if (tab.getPosition() == 0) {
if (fragment_det == null) {
System.out.println("again");
fragment_det = new DetailsFragment();
}
ft.replace(android.R.id.content, fragment_det);
} else {
MapAsyc m = new MapAsyc();
ft.replace(android.R.id.content, m);
}
}
@Override
public void onTabUnselected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
}
@Override
public void onTabReselected(Tab tab,
android.support.v4.app.FragmentTransaction ft) {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_place, menu);
MenuItem actionItem = menu.findItem(R.id.action_share);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem
.getActionProvider();
actionProvider.setShareHistoryFileName(null);
actionProvider.setOnShareTargetSelectedListener(this);
actionProvider.setShareIntent(createShareIntent());
return true;
}
public boolean onShareTargetSelected(ShareActionProvider source,
Intent intent) {
// TODO Auto-generated method stub
this.startActivity(createShareIntent());
return true;
}
private Intent createShareIntent() {
String shareText = "Here is the share content body";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareText);
return sharingIntent;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
case R.id.action_share:
Toast.makeText(this, "Share Via", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
}
DetailsFragment.java
public class DetailsFragment extends SherlockFragment
{
ImageFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
//container.removeAllViews();
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_details, container,
false);
mAdapter = new ImageFragmentAdapter(getActivity().getSupportFragmentManager());
mPager = (ViewPager) view.findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
return view;
}
}
My Tab 1 is working fine. When i click on Tab 2, map will load. But wen i click on tab1 again , i get the following error:::.
12-06 02:30:13.891: E/AndroidRuntime(16904): java.lang.IllegalStateException: Recursive entry to executePendingTransactions.
Some posts suggested to use ASYNC task. But i cant use it because i should return the view from the MapFragment to the listener to take action.
Complete error log
12-06 22:54:58.609: E/AndroidRuntime(28745): FATAL EXCEPTION: main
12-06 22:54:58.609: E/AndroidRuntime(28745): java.lang.IllegalStateException: Recursive entry to executePendingTransactions
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.FragmentManagerImpl.execPendingActions(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.view.ViewPager.populate(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.view.ViewPager.setCurrentItemInternal(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.view.ViewPager.setCurrentItemInternal(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.view.ViewPager.onRestoreInstanceState(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.view.View.dispatchRestoreInstanceState(View.java:12088)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2582)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2588)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.view.View.restoreHierarchyState(View.java:12066)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.Fragment.restoreViewState(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.FragmentManagerImpl.moveToState(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.BackStackRecord.run(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.FragmentManagerImpl.execPendingActions(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.support.v4.app.FragmentManagerImpl$1.run(Unknown Source)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.os.Handler.handleCallback(Handler.java:725)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.os.Handler.dispatchMessage(Handler.java:92)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.os.Looper.loop(Looper.java:137)
12-06 22:54:58.609: E/AndroidRuntime(28745): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-06 22:54:58.609: E/AndroidRuntime(28745): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 22:54:58.609: E/AndroidRuntime(28745): at java.lang.reflect.Method.invoke(Method.java:511)
12-06 22:54:58.609: E/AndroidRuntime(28745): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-06 22:54:58.609: E/AndroidRuntime(28745): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-06 22:54:58.609: E/AndroidRuntime(28745): at dalvik.system.NativeStart.main(Native Method)
回答1:
use http://viewpagerindicator.com/ for your tabview and set
mPager.setOffscreenPageLimit(mAdapter.getCount());
P.S Are you using actionbarsherlok?
来源:https://stackoverflow.com/questions/13741294/how-to-build-an-app-with-actionbar-tab-support-and-mapview