我正在尝试构建一个只显示地图的非常简单的Google地图应用程序。当项目构建正常时,它会强制关闭模拟器。代码:
helloMaps.java:
package com.example.hellomaps;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class helloMaps extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_maps);
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_hello_maps, menu);
return true;
}
}
helloMaps.manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hellomaps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />
<permission
android:name="com.example.hellomaps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.hellomaps.permission.MAPS_RECEIVE"/>
<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" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps"/>
<activity
android:name=".helloMaps"
android:label="@string/title_activity_hello_maps" >
<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.maps.v2.API_KEY"
android:value="api key here"
/>
</application>
</manifest>
activity_hello_maps.xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
启动应用程序会导致classNotFoundException。 logCat输出为:
05-11 10:43:09.614: D/AndroidRuntime(336): Shutting down VM
05-11 10:43:09.614: W/dalvikvm(336): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-11 10:43:09.614: E/AndroidRuntime(336): Uncaught handler: thread main exiting due to uncaught exception
05-11 10:43:09.624: E/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hellomaps/com.example.hellomaps.helloMaps}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
05-11 10:43:09.624: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.os.Looper.loop(Looper.java:123)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-11 10:43:09.624: E/AndroidRuntime(336): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 10:43:09.624: E/AndroidRuntime(336): at java.lang.reflect.Method.invoke(Method.java:521)
05-11 10:43:09.624: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-11 10:43:09.624: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-11 10:43:09.624: E/AndroidRuntime(336): at dalvik.system.NativeStart.main(Native Method)
05-11 10:43:09.624: E/AndroidRuntime(336): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
05-11 10:43:09.624: E/AndroidRuntime(336): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-11 10:43:09.624: E/AndroidRuntime(336): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.app.Activity.setContentView(Activity.java:1622)
05-11 10:43:09.624: E/AndroidRuntime(336): at com.example.hellomaps.helloMaps.onCreate(helloMaps.java:12)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-11 10:43:09.624: E/AndroidRuntime(336): ... 11 more
05-11 10:43:09.624: E/AndroidRuntime(336): Caused by: java.lang.NullPointerException
05-11 10:43:09.624: E/AndroidRuntime(336): at java.lang.VMClassLoader.findLoadedClass(Native Method)
05-11 10:43:09.624: E/AndroidRuntime(336): at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:404)
05-11 10:43:09.624: E/AndroidRuntime(336): at java.lang.ClassLoader.loadClass(ClassLoader.java:563)
05-11 10:43:09.624: E/AndroidRuntime(336): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
05-11 10:43:09.624: E/AndroidRuntime(336): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558)
05-11 10:43:09.624: E/AndroidRuntime(336): ... 19 more
05-11 10:43:09.644: I/dalvikvm(336): threadid=7: reacting to signal 3
05-11 10:43:09.644: E/dalvikvm(336): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
我的问题:
1.我如何使这项工作?
答案 0 :(得分:1)
将其添加到应用程序元素内的清单中:
<uses-library android:name="com.google.android.maps"/>
例如:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps"/>
<activity
android:name=".helloMaps"
android:label="@string/title_activity_hello_maps" >
<intent-filter>
答案 1 :(得分:0)
你正在使用谷歌地图的API混乱。如果您尝试引用android-support-v4.jar
am,假设您正在尝试实施Google Maps API V2。现在,如果是这种情况,那么您的代码中存在很多问题,这些问题很可能导致classNotFoundException
错误。
<permission android:name="your.application.package.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="your.application.package.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" />
接下来,您必须使用MapFragment
或SupportMapFragment
对象,这些对象是Google Maps API v2对象,而不是您正在使用的MapView
对象,即API V1对象。
通过阅读我在此主题上撰写的博客文章,您可以获得有关如何正确实现它的更多信息:
<强>更新强>
1。首先,如果您定位SupportMapFragment
,则无需使用API => 11
。
2。删除此权限:
<uses-library android:name="com.google.android.maps"/>
在 API V2 中不需要 Google Map API V1 权限nad。
3。从你的错误:Error inflating class fragment
中膨胀片段存在一些问题。
首先尝试将片段放置在以下布局中:
<?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" >
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
如果这对您没有帮助,那么在大多数情况下,您在引用Google Map API V2所需的库之前会遇到一些问题,请再次阅读我的指南并确保您正确地执行前4个步骤。