Android 膨胀类片段时出错。
多次尝试避免此错误,但总线仍然找不到此错误的答案。 这是我第一个使用谷歌API的应用程序。我只想在屏幕上显示地图。我得到了膨胀的类片段错误,并尝试了许多我发现的解决方案,但仍然得到了相同的错误。请帮忙 MainActivity.javaAndroid 膨胀类片段时出错。,android,google-maps,android-fragments,google-maps-api-2,Android,Google Maps,Android Fragments,Google Maps Api 2,多次尝试避免此错误,但总线仍然找不到此错误的答案。 这是我第一个使用谷歌API的应用程序。我只想在屏幕上显示地图。我得到了膨胀的类片段错误,并尝试了许多我发现的解决方案,但仍然得到了相同的错误。请帮忙 MainActivity.java package com.example.testmap; import com.google.android.gms.maps.GoogleMap; import android.os.Bundle; import android.app.Activity;
package com.example.testmap;
import com.google.android.gms.maps.GoogleMap;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
import com.google.android.gms.maps.MapFragment;
public class MainActivity extends Activity {
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testmap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<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" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<activity
android:name="com.example.testmap.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>
<!-- Goolge API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAo7D5KmWcB8hdw6QZ_xxxxxxxxxxlv6m0" />
<!--add your api here as android:value -->
</application>
</manifest>
将片段标记替换为以下内容
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
据我所知,MapFragment适用于较旧的API版本。您没有写入外部存储的权限,因此添加
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在您的权限中您也可以添加异常堆栈跟踪吗?但是android:minSdkVersion=14那么使用MapFragments就可以了谢谢您的帮助!!