在真正的Android设备2.3上谷歌地图v2中膨胀类片段?

时间:2014-03-19 07:44:31

标签: android google-maps android-fragments

我不知道如何让我的应用在真实设备中正常运行

我从2年前开始在我的应用中使用谷歌地图v1,但是从谷歌文档说" v1已被弃用"我昨天知道。我想将我的应用程序更改为谷歌地图v2,我在日志显示中遇到问题

03-19 15:07:30.429: E/AndroidRuntime(9540): FATAL EXCEPTION: main
03-19 15:07:30.429: E/AndroidRuntime(9540): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidmapsv2/com.example.androidmapsv2.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.os.Looper.loop(Looper.java:130)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.app.ActivityThread.main(ActivityThread.java:3687)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at java.lang.reflect.Method.invoke(Method.java:507)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at dalvik.system.NativeStart.main(Native Method)
03-19 15:07:30.429: E/AndroidRuntime(9540): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.app.Activity.setContentView(Activity.java:1657)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at com.example.androidmapsv2.MainActivity.onCreate(MainActivity.java:24)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
03-19 15:07:30.429: E/AndroidRuntime(9540):     ... 11 more
03-19 15:07:30.429: E/AndroidRuntime(9540): Caused by: java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:
03-19 15:07:30.429: E/AndroidRuntime(9540): <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
03-19 15:07:30.429: E/AndroidRuntime(9540):     at maps.e.ch.a(Unknown Source)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at maps.e.ay.a(Unknown Source)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at maps.e.ay.a(Unknown Source)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at maps.e.al.a(Unknown Source)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at maps.e.bg.a(Unknown Source)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at maps.e.bf.a(Unknown Source)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at eio.onTransact(SourceFile:107)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.os.Binder.transact(Binder.java:279)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at com.google.android.gms.dynamic.a$4.b(Unknown Source)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at com.google.android.gms.dynamic.a.a(Unknown Source)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:884)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1168)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
03-19 15:07:30.429: E/AndroidRuntime(9540):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
03-19 15:07:30.429: E/AndroidRuntime(9540):     ... 20 more

我在这个论坛上知道很多这样的问题,但是所有答案都无法解决问题,我从任何链接和谷歌文档尝试过教程但从未成功过,我认为我的问题在我的手机中,我使用android 2.3.6 gingerbread(真实设备)。

这个mainactivity.java

public class MainActivity extends FragmentActivity {

    final int RQS_GooglePlayServices = 1;
    private GoogleMap myMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         FragmentManager myFragmentManager = getSupportFragmentManager();
         SupportMapFragment mySupportMapFragment 
            = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
         myMap = mySupportMapFragment.getMap();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_legalnotices:
            String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
                    getApplicationContext());
            AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
            LicenseDialog.setTitle("Legal Notices");
            LicenseDialog.setMessage(LicenseInfo);
            LicenseDialog.show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

        if (resultCode == ConnectionResult.SUCCESS){
            Toast.makeText(getApplicationContext(), 
                    "isGooglePlayServicesAvailable SUCCESS", 
                    Toast.LENGTH_LONG).show();
        }else{
            GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
        }

    }

}

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidmapsv2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <permission 
        android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE" 
        android:protectionLevel="signature"></permission>
    <uses-permission 
        android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE"/>
    <uses-permission 
        android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission 
        android:name="android.permission.INTERNET"/>
    <uses-permission 
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <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"/>

    <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.maps.v2.API_KEY"
            android:value="AIzaSyBD9fsiMd_G9Pzeq2Eqas2FwJjzMGbSOnA"/>
        <activity
            android:name="com.example.androidmapsv2.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>
    </application>

</manifest>

activity_main.xml中

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>

也许有人跟我有同样的问题?你是怎么解决这个问题的?

感谢

2 个答案:

答案 0 :(得分:1)

  

引起:java.lang.SecurityException:Maps API需要   在AndroidManifest.xml中设置其他以下权限   确保正确的行为:03-19 15:07:30.429:   E / AndroidRuntime(9540):

在清单

中添加此权限
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/>

可以在您定义了其他权限的地方添​​加上述内容

我已经建议将以下内容添加为应用程序标记的子项

<meta-data 
    android:name="com.google.android.gms.version" 
    android:value="@integer/google_play_services_version" />

答案 1 :(得分:0)

在清单中,将其放在应用程序定义的末尾,就在应用程序关闭之前:

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyBD9fsiMd_G9Pzeq2Eqas2FwJjzMGbSOnA"/>

就在此之前,还要说明:

    <meta-data 
        android:name="com.google.android.gms.version" 
        android:value="@integer/google_play_services_version" />

有了这一切,如果你已经完成了所有创建API密钥和所有这些东西,你应该有这个woirking