我想写一个简单的地图程序。我正在使用eclipse。我得到以下例外情况:
05-06 10:25:33.474: E/AndroidRuntime(7341): FATAL EXCEPTION: main
05-06 10:25:33.474: E/AndroidRuntime(7341): Process: com.example.mapapp, PID: 7341
05-06 10:25:33.474: E/AndroidRuntime(7341): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapapp/com.example.mapapp.MapAppHome}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.os.Handler.dispatchMessage(Handler.java:102)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.os.Looper.loop(Looper.java:136)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-06 10:25:33.474: E/AndroidRuntime(7341): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 10:25:33.474: E/AndroidRuntime(7341): at java.lang.reflect.Method.invoke(Method.java:515)
05-06 10:25:33.474: E/AndroidRuntime(7341): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-06 10:25:33.474: E/AndroidRuntime(7341): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-06 10:25:33.474: E/AndroidRuntime(7341): at dalvik.system.NativeStart.main(Native Method)
05-06 10:25:33.474: E/AndroidRuntime(7341): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
05-06 10:25:33.474: E/AndroidRuntime(7341): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.app.Activity.setContentView(Activity.java:1929)
05-06 10:25:33.474: E/AndroidRuntime(7341): at com.example.mapapp.MapAppHome.onCreate(MapAppHome.java:13)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.app.Activity.performCreate(Activity.java:5231)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-06 10:25:33.474: E/AndroidRuntime(7341): ... 11 more
05-06 10:25:33.474: E/AndroidRuntime(7341): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.app.Fragment.instantiate(Fragment.java:597)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.app.Fragment.instantiate(Fragment.java:561)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.app.Activity.onCreateView(Activity.java:4778)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
05-06 10:25:33.474: E/AndroidRuntime(7341): ... 21 more
05-06 10:25:33.474: E/AndroidRuntime(7341): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: DexPathList[[zip file "/data/app/com.example.mapapp-2.apk"],nativeLibraryDirectories=[/data/app- lib/com.example.mapapp-2, /vendor/lib, /system/lib]]
05-06 10:25:33.474: E/AndroidRuntime(7341): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
05-06 10:25:33.474: E/AndroidRuntime(7341): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
05-06 10:25:33.474: E/AndroidRuntime(7341): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
05-06 10:25:33.474: E/AndroidRuntime(7341): at android.app.Fragment.instantiate(Fragment.java:583)
05-06 10:25:33.474: E/AndroidRuntime(7341): ... 24 more
这是我的android清单代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapapp"
android:versionCode="1"
android:versionName="1.0" >
<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"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mapapp.MapAppHome"
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_code"/>
</application>
这是我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.maps.MapFragment"/>
</LinearLayout>
这是我的活动代码:
package com.example.mapapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MapAppHome extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_app_home);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map_app_home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
google play services lib在eclipse的工作区中并被引用(对mapapp属性),如果它在工作区文件夹中,则无关紧要(相同的错误)。
我有一个有效的api密钥,我这次在android studio的同一个项目中使用它并且它工作得很好。
项目构建api设置为Google api 4.4.2,并在项目属性中的java构建路径中按顺序和导出选项卡,检查所有选项(Google Api,android私有库和android dependecies)。
正如我之前所说,相同的代码与必要的(grandle)更改完美地工作。我的所有sdk文件都是最新的。
我缺少什么?
答案 0 :(得分:0)
Extend your activity with FragmentActivity
public class MapAppHome extends FragmentActivity {
GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initalization();
createMapIfReady();
}
@Override
protected void onResume() {
super.onResume();
createMapIfReady();
}
private void createMapIfReady() {
// TODO Auto-generated method stub
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mMap = mapFragment.getMap();
}
}
public void initalization() {
int status = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getBaseContext());
if (status == ConnectionResult.SUCCESS) {
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mMap = supportMapFragment.getMap();
}
else {
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
requestCode);
dialog.show();
}
}
and in you xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>