android - 如何正确初始化 map ?

标签 android android-fragments google-maps-api-2 android-maps-v2

如何在 fragment 中初始化 map ?我现在得到一个空指针异常。我有一个 fragment Fragment01,它只用于显示 map 和一个布局 Fragment_01.xml,它只包含 map 。

list

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />

<permission
android:name="se.sebastianliljegren.nellienovafoto.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>

<uses-permission android:name="se.sebastianliljegren.nellienovafoto.permission.MAPS_RECEIVE"/>
<library
    android:name="com.google.android.maps"
    />
<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"/>

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

    <meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="############################"/>

    <activity
        android:name="se.sebastianliljegren.nellienovafoto.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

主要 Activity 公共(public)类 MainActivity 扩展 FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    StartFragment startfragment = new StartFragment();

    transaction.add(R.id.fragment_placeholder, startfragment);
    transaction.commit();
}

public void onSelectFragment(View view){

    Fragment newFragment;

    if (view == findViewById(R.id.btnomforetaget)) {
        newFragment = new StartFragment();
    } else if (view == findViewById(R.id.btnhittahit)) {
        newFragment = new Fragment01();
    } else if (view == findViewById(R.id.btnhemsida)) {
        newFragment = new Fragment02();
    } else {
        newFragment = new StartFragment();
    }

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_placeholder, newFragment);
    transaction.addToBackStack(null);
    transaction.commit();

}

}

fragment 01

public class Fragment01 extends Fragment {
MapView mapView;
GoogleMap map;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_01, container, false);

LatLng sydney = new LatLng(-33.867, 151.206);

map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

map.addMarker(new MarkerOptions()
    .title("Sydney")
    .snippet("The most populous city in Australia.")
    .position(sydney));

return view;
}

@Override
public void onDestroyView()
{
  super.onDestroyView();
   Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));  
  FragmentTransaction ft =     getActivity().getSupportFragmentManager().beginTransaction();
  ft.remove(fragment);
  ft.commit();

}

}

Fragment_01.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/fragment02">
<fragment android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.MapFragment"/>
</LinearLayout>

最佳答案

你的 android:minSdkVersion="11" 所以你应该改变它

 <fragment android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:name="com.google.android.gms.maps.SupportMapFragment"/> 

也改变这个

 map= ((MapFragment) getFragmentManager().findFragmentById(
            R.id.map)).getMap();

 map= ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
            R.id.map)).getMap();

同时更改你的 onDestroyView() 如下:

 @Override
    public void onDestroyView()
    {
        try{
          SupportMapFragment fragment = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map));
          FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
          ft.remove(fragment);
          ft.commit();
        }catch(Exception e){
        }
      super.onDestroyView();  
  }

同时在您的 Fragmnt01

中导入以下
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import com.google.android.gms.maps.SupportMapFragment;

关于android - 如何正确初始化 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24008643/

相关文章:

java - Google Maps API v2 显示空白 map

android - 如何使用 PagerAdapter 替换 ViewPager 中的 Fragment?

android - 通过单击/触摸外部 : 删除 fragment

android - 如何在布局(不是 Activity )之间添加过渡

android - Android- fragment 中的Firebase Analytics

java - 当 TextView 在 fragment 内的 onLocationChanged 内更改时应用程序崩溃

android - 仅将横向模式设置为 Android Google Map Fragment

java - 如何从 android 中的 Google Map v2 中清除圆圈和标记

android - 在ExoPlayer中复制音频ByteBuffer

javascript - 使用 JavaScript 的原生 Android DatePicker