I search all days and I can't find the answer...
everything is ready but google map still can't show on fragment with tab
I check that I already turn on google map android v2 and right API_KEY
I have no idea what happened!
MainActivity.java:
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String titles[] = { this.getString(R.string.title_around),
this.getString(R.string.title_map),
this.getString(R.string.title_favorite),
this.getString(R.string.title_achievement), };
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < titles.length; i++) {
actionBar.addTab(actionBar.newTab().setText(titles[i])
.setTabListener(this));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// TODO 在這邊決定哪個位置要給什麼 View(Fragment)
Fragment fragment = null;
// TODO 建議先在之前就初始化好 4 個 Fragment
switch (position) {
case 0:
fragment = new AroundFragment();
break;
case 1:
fragment = new MapsFragment();
break;
case 2:
fragment = new FavoriteFragment();
break;
case 3:
fragment = new AchievementFragment();
break;
default:
break;
}
return fragment;
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
// TODO 在這邊決定每一個 View 的 title
switch (position) {
case 0:
return getString(R.string.title_around);
case 1:
return getString(R.string.title_map);
case 2:
return getString(R.string.title_favorite);
case 3:
return getString(R.string.title_achievement);
}
return null;
}
}
}
MapsFragment.java:
public class MapsFragment extends Fragment{
static final LatLng NKUT = new LatLng(23.979548, 120.696745);
private GoogleMap map;
SupportMapFragment mMapFragment;
public MapsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.map_fragment,container, false);
map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.map,mMapFragment);
fragmentTransaction.commit();
return rootView;
}
}
Map_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
Manifest.xml:
<!-- Google Map -->
<permission
android:name="com.jertt.yummymap.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.jertt.yummymap.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- end -->
<!-- Google Map API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="key" />
<!-- end -->
Solution:
I found the solution!!!
If you use private keystore to apply API_KEY. You cannot upload and install application with eclipse.
You need to upload .apk file to your device by yourself and install it!
Thanks Tarsem and srikanth gr help!!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…