Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid ActionBarSherlock tabbed action bar with a mapview
    text
    copied!<p>I have 4 views, two of which contain mapviews. What I really want in the end is a tabbed navigation between these 4 views.</p> <p>After some research, it seams like the best idea would be to use ActionBarSherlock and make each one of these views a "Fragment" inside a master "Main" Activity. This seams like a very easy to do thing but is causing me massive amounts of trouble (indeed, seams to be a pain for everyone from what I can find online).</p> <p>First, my code. <strong>ActivityMain.java</strong></p> <pre><code>public class ActivityMain extends SherlockFragmentActivity { static String CLASS_NAME = "ActivityMain"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { Log.v(CLASS_NAME, "here"); super.onCreate(savedInstanceState); setContentView(R.layout.main); this.createMenu(); } public void createMenu() { this.getSupportActionBar().setDisplayShowHomeEnabled(false); this.getSupportActionBar().setDisplayShowTitleEnabled(false); this.getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); Tab tab1 = getSupportActionBar() .newTab() .setText("First tab") .setTabListener(new MyTabListener&lt;FragmentOne&gt;(this, FragmentOne.class)); getSupportActionBar().addTab(tab1); Tab tab2 = getSupportActionBar() .newTab() .setText("Second tab") .setTabListener(new MyTabListener&lt;FragmentTwo&gt;(this, FragmentTwo.class)); getSupportActionBar().addTab(tab2); } public class MyTabListener&lt;T extends Fragment&gt; implements TabListener { private ActivityMain mActivity; private Fragment mFragment; private Class&lt;T&gt; mClass; public MyTabListener(ActivityMain activity, Class&lt;T&gt; fragmentClass) { mActivity = activity; mClass = fragmentClass; } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { if (mFragment == null) { mFragment = Fragment.instantiate(mActivity, mClass.getName()); ft.add(android.R.id.content, mFragment, null); } else { //ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.animationtest); ft.attach(mFragment); } } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { if (mFragment != null) { ft.detach(mFragment); } } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { } } } </code></pre> <p>FragmentOne loads just fine, no problems on that one. FragmentTwo is where the trouble begins. Here is the files: <strong>FragmentTwo.java</strong></p> <pre><code>package com.test; import com.actionbarsherlock.app.SherlockFragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class FragmentTwo extends SherlockFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v("Test", "Two!"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.two, container, false); return view; } } </code></pre> <p>and the corresponding layout looks like this:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mapMapView" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="0.40" android:clickable="true" android:apiKey="MY_KEY" /&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@android:style/ButtonBar" android:layout_width="match_parent" android:layout_height="wrap_content" &gt; &lt;Button android:id="@+id/refreshButton" android:layout_width="match_parent" android:layout_height="fill_parent" android:onClick="refreshSightings" android:text="@string/A_MAP_REFRESH_BUTTON" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>Now of course this is giving me the error <em>Binary XML file line #7: Error inflating class com.google.android.maps.MapView</em></p> <p>I have tried importing the <a href="https://github.com/petedoyle/android-support-v4-googlemaps" rel="nofollow">https://github.com/petedoyle/android-support-v4-googlemaps</a> package and replacing the already existing android-support-v4 package but when I try to run my app, it dies with the error that it can't find the ActivityMain class.</p> <p>First question, am I even going about this in the right way? Should I be using this 1 main activity and loading all the other things in Fragments?</p> <p>Next, if I am doing this in a correct way, is there a <em>good</em> tutorial for how to get this to work in a decent way? I can't find anything online except for other people asking the same questions and a few tutorials that haven't worked for me.</p> <p>Basically, how can I have tabbed navigation with 2 maps in 2 different tabs?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload