我正努力让我的Google Map API v2片段发挥作用。我已经仔细检查了API密钥和权限,但是在尝试显示地图时我仍然得到null。以下是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hanu.eateries"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.hanu.eateries.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.hanu.eateries.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.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:allowBackup="true"
android:uiOptions="splitActionBarWhenNarrow" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/mapApiKey" />
<activity
android:name=".DashboardActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity" />
<activity android:name=".RegisterActivity" />
<activity android:name=".PlaceListActivity" />
<activity android:name=".WelcomeActivity" />
<activity android:name=".PlaceViewActivity" />
<activity android:name=".AccountActivity"/>
<activity android:name=".SettingsActivity" />
<activity android:name=".SettingsActivityLogin" />
</application>
</manifest>
这是我的代码:
public class PlaceLocationFragment extends Fragment{
private GoogleMap myMap;
final int RQS_GooglePlayServices = 1;
private static final double GEO_CONVERSION = 1E6;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.location_view, container, false);
initLayout();
return view;
}
private void initLayout(){
FragmentManager myFragmentManager = getActivity().getSupportFragmentManager();
SupportMapFragment mySupportMapFragment = (SupportMapFragment) myFragmentManager.findFragmentById(R.id.map);
myMap = mySupportMapFragment.getMap();
myMap.setMyLocationEnabled(true);
myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
Intent intent = getActivity().getIntent();
double spotLat = intent.getDoubleExtra("LAT", 21.139);
double spotLng = intent.getDoubleExtra("LNG", 105.851);
int latitude = (int) (spotLat * GEO_CONVERSION);
int longitude = (int) (spotLng * GEO_CONVERSION);
LatLng point = new LatLng(latitude, longitude);
myMap.addMarker(new MarkerOptions().position(point).title(intent.getStringExtra("NAME") + "\n" + intent.getStringExtra("ADD")));
myMap.getUiSettings().setCompassEnabled(true);
myMap.getUiSettings().setZoomControlsEnabled(true);
myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
}
@Override
public void onResume(){
super.onResume();
super.onResume();
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity().getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getActivity().getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, getActivity(), RQS_GooglePlayServices);
}
}
}
Logcat将错误指向行myMap.setMyLocationEnabled(true);
:
03-09 13:52:31.101: E/AndroidRuntime(802): FATAL EXCEPTION: main
03-09 13:52:31.101: E/AndroidRuntime(802): java.lang.NullPointerException
03-09 13:52:31.101: E/AndroidRuntime(802): at com.hanu.eateries.PlaceLocationFragment.initLayout(PlaceLocationFragment.java:39)
03-09 13:52:31.101: E/AndroidRuntime(802): at com.hanu.eateries.PlaceLocationFragment.onCreateView(PlaceLocationFragment.java:30)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:523)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:495)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:476)
03-09 13:52:31.101: E/AndroidRuntime(802): at com.hanu.eateries.PlaceViewActivity$2.onTabSelected(PlaceViewActivity.java:63)
03-09 13:52:31.101: E/AndroidRuntime(802): at com.actionbarsherlock.internal.app.ActionBarWrapper$TabWrapper.onTabSelected(ActionBarWrapper.java:344)
03-09 13:52:31.101: E/AndroidRuntime(802): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
03-09 13:52:31.101: E/AndroidRuntime(802): at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1067)
03-09 13:52:31.101: E/AndroidRuntime(802): at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:519)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.view.View.performClick(View.java:4202)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.view.View$PerformClick.run(View.java:17340)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.os.Handler.handleCallback(Handler.java:725)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.os.Handler.dispatchMessage(Handler.java:92)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.os.Looper.loop(Looper.java:137)
03-09 13:52:31.101: E/AndroidRuntime(802): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-09 13:52:31.101: E/AndroidRuntime(802): at java.lang.reflect.Method.invokeNative(Native Method)
03-09 13:52:31.101: E/AndroidRuntime(802): at java.lang.reflect.Method.invoke(Method.java:511)
03-09 13:52:31.101: E/AndroidRuntime(802): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-09 13:52:31.101: E/AndroidRuntime(802): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-09 13:52:31.101: E/AndroidRuntime(802): at dalvik.system.NativeStart.main(Native Method)
编辑:我的location_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
这可能是什么问题?如果您有任何建议,请让我知道。谢谢。
答案 0 :(得分:0)
实际上我认为问题在于你试图将MapFragment
放在Fragment
内。不允许Fragment
级联。您的所有地图/标记操作代码:
FragmentManager myFragmentManager = getActivity().getSupportFragmentManager();
SupportMapFragment mySupportMapFragment = (SupportMapFragment) myFragmentManager.findFragmentById(R.id.map);
myMap = mySupportMapFragment.getMap();
myMap.setMyLocationEnabled(true);
myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
Intent intent = getActivity().getIntent();
double spotLat = intent.getDoubleExtra("LAT", 21.139);
double spotLng = intent.getDoubleExtra("LNG", 105.851);
int latitude = (int) (spotLat * GEO_CONVERSION);
int longitude = (int) (spotLng * GEO_CONVERSION);
LatLng point = new LatLng(latitude, longitude);
myMap.addMarker(new MarkerOptions().position(point).title(intent.getStringExtra("NAME") + "\n" + intent.getStringExtra("ADD")));
myMap.getUiSettings().setCompassEnabled(true);
myMap.getUiSettings().setZoomControlsEnabled(true);
myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
应该包含使用包含MapFragment
的布局的Activity / FragmentActivity。
答案 1 :(得分:0)
原因是这不起作用的原因是我将API密钥引用到字符串。当我尝试将密钥直接放在Manifest文件中时,它可以工作!这很奇怪,但我很高兴问题现在已经解决了。