我是stackoverflow的新手 我有使用的谷歌地图测试项目 here 我喜欢教程,但应用程序崩溃。
我完成了教程中的每一步,获得了android键,添加了库等等: 我的类似问题是这个链接: here 但它没有任何有效的答案。 我完全删除eclips,卸载java JDK和JRE,甚至我的整个系统java。但应用程序崩溃了。 任何身体可以帮助我吗?非常感谢你
这里是我的代码:
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
public class MainActivity extends FragmentActivity {
private GoogleMap googleMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void initilizeMap() {
if (googleMap == null) {
//googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap = ((SupportMapFragment) getSupportFragmentManager().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();
}
}
}
}
和清单文件是:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.maptest"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.test.maptest.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.test.maptest.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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>
</application>
<!-- Goolge API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBYju6h2BWvZOaSDQpe5f9tv6fJsZy6cY8" />
</manifest>
和布局是:
<?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>
这是logcat:
11-25 14:04:50.663: E/Trace(1411): error opening trace file: No such file or directory (2)
11-25 14:04:50.853: D/AndroidRuntime(1411): Shutting down VM
11-25 14:04:50.853: W/dalvikvm(1411): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-25 14:04:50.873: E/AndroidRuntime(1411): FATAL EXCEPTION: main
11-25 14:04:50.873: E/AndroidRuntime(1411): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ariagostar.maptest/com.ariagostar.maptest.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.os.Looper.loop(Looper.java:137)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-25 14:04:50.873: E/AndroidRuntime(1411): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 14:04:50.873: E/AndroidRuntime(1411): at java.lang.reflect.Method.invoke(Method.java:511)
11-25 14:04:50.873: E/AndroidRuntime(1411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-25 14:04:50.873: E/AndroidRuntime(1411): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-25 14:04:50.873: E/AndroidRuntime(1411): at dalvik.system.NativeStart.main(Native Method)
11-25 14:04:50.873: E/AndroidRuntime(1411): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-25 14:04:50.873: E/AndroidRuntime(1411): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.app.Activity.setContentView(Activity.java:1867)
11-25 14:04:50.873: E/AndroidRuntime(1411): at com.ariagostar.maptest.MainActivity.onCreate(MainActivity.java:25)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.app.Activity.performCreate(Activity.java:5008)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-25 14:04:50.873: E/AndroidRuntime(1411): ... 11 more
11-25 14:04:50.873: E/AndroidRuntime(1411): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
11-25 14:04:50.873: E/AndroidRuntime(1411): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
答案 0 :(得分:0)
更改
public class MainActivity extends FragmentActivity {
到
public class MainActivity extends Activity {
更改此
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
到
googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
您还需要清单
中的应用程序标记中的以下内容<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
这是google play services rev 13的要求
编辑:
移动
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBYju6h2BWvZOaSDQpe5f9tv6fJsZy6cY8" />
清单
中的应用程序内部标记答案 1 :(得分:0)
试试这个。
您的MainActivity
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Valida si el mapa no se creo correctamente
if (googleMap == null) {
Toast.makeText(getApplicationContext(), "Sorry! el mapa no pudo ser cargado", Toast.LENGTH_SHORT)
.show();
}
}
}
在你的布局中
<?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:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
在清单中,我在MAC中使用了这些行,因为在Windows中正常工作没有它们
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
答案 2 :(得分:0)
我有类似的问题。我添加:
import com.google.android.gms.maps.SupportMapFragment;
将xml更改为
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/header" />
解决了这个问题。我希望你也解决它!