0

我正在努力使我的 Google Map API v2 片段正常工作。我已经仔细检查了 API 密钥和权限,但在尝试显示地图时仍然为空。以下是我的清单:

<?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"/>

这里可能是什么问题?如果您有任何建议,请让我知道。谢谢你。

4

2 回答 2

0

实际上,我认为问题在于您试图将 aMapFragment放入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));

应该进入使用包含您的布局的 Activity/FragmentActivity 内部MapFragment

于 2013-03-09T15:00:55.063 回答
0

原来这不起作用的原因是我将 API 密钥引用到了一个字符串。当我尝试将密钥直接放在清单文件中时,它可以工作!这很奇怪,但我很高兴问题现在解决了。

于 2013-03-10T03:20:55.043 回答