使用 google 地图时膨胀 class 片段时出错
Error inflating class fragment when using google maps
我正在尝试在我的 android 应用程序中实现 google 地图。我遵循了 this 指南,但我得到了 "Error inflating class fragment",我不知道哪里出了问题。
我的布局文件:
<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"/>
Activity:
public class MainActivity extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
}
}
清单(用 "my_api_key" 替换了我真正的 api 密钥):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test3"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.example.test3.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.test3.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
<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="my_api_key" />
</application>
</manifest>
试试这个
SupportMapFragment map =(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
map.getMapAsync(this);
让 MainActivity 实现 OnMapReadyCallback。
我们在地图准备好后通过回调获取地图对象
@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
}`
好吧,我已经在我的应用程序中实现了 google 地图 api v2
试试这个代码
xml 文件
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.MapFragment"/>
class 文件
//extends activity and implements onMapReady
public class map_activity extends Activity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myfile_layout);
}
@Override
public void onMapReady(GoogleMap googleMap) {
//do everything with the map
//change the map type
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// Creating a LatLng object for the current locati
LatLng latLng = new LatLng(23.634501, -102.552784);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
如果这对您有帮助,请不要忘记将此答案设为正确...
感谢您的回复,我刚刚弄清楚我做错了什么。我在 phone 中启用了 USB 数据传输到我的电脑,这就是应用程序崩溃的原因。不敢相信我犯了这样一个新手错误。
我正在尝试在我的 android 应用程序中实现 google 地图。我遵循了 this 指南,但我得到了 "Error inflating class fragment",我不知道哪里出了问题。
我的布局文件:
<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"/>
Activity:
public class MainActivity extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
}
}
清单(用 "my_api_key" 替换了我真正的 api 密钥):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test3"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.example.test3.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.test3.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
<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="my_api_key" />
</application>
</manifest>
试试这个
SupportMapFragment map =(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
map.getMapAsync(this);
让 MainActivity 实现 OnMapReadyCallback。 我们在地图准备好后通过回调获取地图对象
@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
}`
好吧,我已经在我的应用程序中实现了 google 地图 api v2
试试这个代码
xml 文件
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.MapFragment"/>
class 文件
//extends activity and implements onMapReady
public class map_activity extends Activity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myfile_layout);
}
@Override
public void onMapReady(GoogleMap googleMap) {
//do everything with the map
//change the map type
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// Creating a LatLng object for the current locati
LatLng latLng = new LatLng(23.634501, -102.552784);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
如果这对您有帮助,请不要忘记将此答案设为正确...
感谢您的回复,我刚刚弄清楚我做错了什么。我在 phone 中启用了 USB 数据传输到我的电脑,这就是应用程序崩溃的原因。不敢相信我犯了这样一个新手错误。