尝试在谷歌地图上工作
- 这是我使用谷歌地图 API 的第一个程序
- 由于日志错误,我无法执行
- 我需要纠正自己的地方
MainActivity.java
public class MainActivity extends Activity {
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
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
protected void onResume() {
super.onResume();
initilizeMap();
}
}
activity_main.xml
<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="android.app.DialogFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
显现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemaps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.googlemaps.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 Maps API Key -->
<meta-data
android:name="com.example.googlemaps.API_KEY"
android:value="------------My-Key----------------" />
</application>
</manifest>
UPDATED_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>
更新日志
10-17 10:57:50.547: I/dalvikvm(458): Could not find method com.example.googlemaps.MainActivity.getFragmentManager, referenced from method com.example.googlemaps.MainActivity.initilizeMap
10-17 10:57:50.547: W/dalvikvm(458): VFY: unable to resolve virtual method 4829: Lcom/example/googlemaps/MainActivity;.getFragmentManager ()Landroid/app/FragmentManager;
10-17 10:57:50.547: D/dalvikvm(458): VFY: replacing opcode 0x6e at 0x0004
10-17 10:57:50.547: D/dalvikvm(458): VFY: dead code 0x0007-0028 in Lcom/example/googlemaps/MainActivity;.initilizeMap ()V
10-17 10:57:50.627: D/AndroidRuntime(458): Shutting down VM
10-17 10:57:50.627: W/dalvikvm(458): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-17 10:57:50.657: E/AndroidRuntime(458): FATAL EXCEPTION: main
10-17 10:57:50.657: E/AndroidRuntime(458): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemaps/com.example.googlemaps.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
10-17 10:57:50.657: E/AndroidRuntime(458): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.os.Looper.loop(Looper.java:123)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-17 10:57:50.657: E/AndroidRuntime(458): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 10:57:50.657: E/AndroidRuntime(458): at java.lang.reflect.Method.invoke(Method.java:507)
10-17 10:57:50.657: E/AndroidRuntime(458): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-17 10:57:50.657: E/AndroidRuntime(458): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-17 10:57:50.657: E/AndroidRuntime(458): at dalvik.system.NativeStart.main(Native Method)
10-17 10:57:50.657: E/AndroidRuntime(458): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
10-17 10:57:50.657: E/AndroidRuntime(458): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-17 10:57:50.657: E/AndroidRuntime(458): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.app.Activity.setContentView(Activity.java:1657)
10-17 10:57:50.657: E/AndroidRuntime(458): at com.example.googlemaps.MainActivity.onCreate(MainActivity.java:19)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-17 10:57:50.657: E/AndroidRuntime(458): ... 11 more
10-17 10:57:50.657: E/AndroidRuntime(458): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.googlemaps-2.apk]
10-17 10:57:50.657: E/AndroidRuntime(458): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
10-17 10:57:50.657: E/AndroidRuntime(458): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
10-17 10:57:50.657: E/AndroidRuntime(458): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
10-17 10:57:50.657: E/AndroidRuntime(458): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
10-17 10:57:50.657: E/AndroidRuntime(458): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
10-17 10:57:50.657: E/AndroidRuntime(458): ... 20 more
有任何想法吗
谢谢