我搜索了一天,我找不到答案...
一切准备就绪,但谷歌地图仍然无法在带有标签的片段上显示
我检查我是否已经打开了 google map android v2 和正确的 API_KEY
我不知道发生了什么!
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>
清单.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 -->
解决方案:
我找到了解决方案!!!
如果您使用私钥库来应用 API_KEY。您不能使用 eclipse 上传和安装应用程序。
您需要自己上传 .apk 文件到您的设备并安装!
感谢 Tarsem 和 srikanth gr 的帮助!!