0

I am building an app which looks something like this.enter image description here

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)
suresh cheemalamudi
  • 6,190
  • 11
  • 49
  • 67
  • did you add google-play-services.jar in your project ? – rajpara Dec 06 '12 at 10:17
  • ya i have added. But i dont know how to use it right. they have just told that adding maps is as easy as adding an fragment in your layout. But How can i use that in tab.? – suresh cheemalamudi Dec 06 '12 at 10:21
  • this is official guide https://developers.google.com/maps/documentation/android/ and also check this answer if it may help you http://stackoverflow.com/a/13721895/582571 – rajpara Dec 06 '12 at 10:24
  • @rajpara:Thanks for the help. Will go thru the links. – suresh cheemalamudi Dec 06 '12 at 10:26
  • 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. – suresh cheemalamudi Dec 06 '12 at 10:32
  • 1
    Well Suresh its hard to tell anything without go through your code, If you post related code & error log then other people will look into that and guide you well – rajpara Dec 06 '12 at 10:34
  • Android does not use the Google Maps API V3. (tag removed) – Marcelo Dec 06 '12 at 10:42
  • you may like to check these examples [TAB View](http://www.rdcworld-android.blogspot.in/2011/11/tabwidget-in-android-advance.html) and [Google_Map] (http://rdcworld-android.blogspot.in/2012/07/google-map-tutorial-android-advance.html) – swiftBoy Dec 06 '12 at 11:34
  • You need to post more information if you want help. Post all of your LogCat errors and the relevant code from MyTabListener. The one LogCat line is clear you have created a loop where one fragment transaction calls another which calls the first, but we cannot help you beyond that with the current information. – Sam Dec 07 '12 at 06:26
  • @Sam I have edited my question. Can u please have a look at it and suggest how to go forward – suresh cheemalamudi Dec 07 '12 at 08:35

1 Answers1

1

use http://viewpagerindicator.com/ for your tabview and set

mPager.setOffscreenPageLimit(mAdapter.getCount());

P.S Are you using actionbarsherlok?

Artem Zelinskiy
  • 2,201
  • 16
  • 19
  • :Ya i am using actionbarsherlok. Is the problem with ActionBarSherlock.? – suresh cheemalamudi Dec 06 '12 at 10:41
  • It seems the problem was in wrong implemented tqab bar. There is a map fragment in support library, so i think it works ok. You should take tab bar implementation from here http://developer.android.com/tools/samples/index.html P.S I prefer viewpageindicator over tabbar – Artem Zelinskiy Dec 06 '12 at 10:47