【问题标题】:error with google maps, caused by meta data谷歌地图错误,由元数据引起
【发布时间】:2016-03-25 13:25:15
【问题描述】:

我在尝试使用选项卡中实现的谷歌地图制作选项卡式布局时出错

我收到以下错误

Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment

Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml

Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment

我继续从谷歌获取我的 api 密钥,这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hp_user.dn_tab1">

<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 ACCESS_COARSE/FINE_LOCATION 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" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme.NoActionBar">

    <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="@string/google_maps_key" />

    <activity android:name=".MainActivity">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


</application>

</manifest>

并为 api 创建了一个值 xml,这给了我一个错误声明 “必须声明元素字符串”

<?xml version="1.0" encoding="utf-8"?>
<string name="google_maps_key" translatable="false" templateMergeStrategy="preserve">
!AIzaSyBIR8JqMnTpCaul4oX1HzgcR-HoJVR85CA
</string>

这一切最初是在我注意到操作栏被弃用并试图将其更改为主要活动中的工具栏时开始的,因为这将允许它从我得到的原始错误运行,即需要停止调用窗口操作栏

主要活动 xml 文件

<RelativeLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    />
<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    app:tabMode="fixed"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    app:tabTextColor="#d3d3d3"
    app:tabSelectedTextColor="#ffffff"
    app:tabIndicatorColor="#ff00ff"

    />


<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

</RelativeLayout>

主要活动java类

public class MainActivity extends ActionBarActivity {

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

    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setTitle("Toolbar Title");
    getSupportActionBar().setSubtitle("Toolbar Subtitle");

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    PagerAdapter pagerAdapter =
            new PagerAdapter(getSupportFragmentManager(), MainActivity.this);
    viewPager.setAdapter(pagerAdapter);

    // Give the TabLayout the ViewPager
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(viewPager);

    // Iterate over all tabs and set the custom view
    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        tab.setCustomView(pagerAdapter.getTabView(i));
    }
}

class PagerAdapter extends FragmentPagerAdapter {

    String tabTitles[] = new String[] { "Home", "Shouts", "Maps", };
    Context context;

    public PagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public int getCount() {
        return tabTitles.length;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return new HomeFragment();
            case 1:
                return new ShoutsFragment();
            case 2:
                return new MapFragment();
        }

        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }

    public View getTabView(int position) {
        View tab = LayoutInflater.from(MainActivity.this).inflate(R.layout.mapfragment, null);
        TextView tv = (TextView) tab.findViewById(R.id.map);
        tv.setText(tabTitles[position]);
        return tab;
    }

}
}

我现在卡住了,因为我是 android 新手,现在不再明白,可以做些什么来让它运行而不因错误而崩溃。

Gradle

   apply plugin: 'com.android.application'

   android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "com.example.hp_user.dn_tab1"
    minSdkVersion 23
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.google.android.gms:play-services:8.4.0'
compile 'com.android.support:design:23.2.1'
compile 'com.android.support:support-v4:23.1.1'

}

【问题讨论】:

  • 尝试将您的&lt;string ...&gt; 放在同一行。
  • 你是否也尝试过重建你的项目?

标签: android api google-maps android-actionbar metadata


【解决方案1】:

尝试改变:

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

<meta-data android:name="com.google.android.geo.API_KEY"
               android:value="@string/google_maps_key"/>

【讨论】:

  • 什么也没做,重建后我的清单在清单中出现错误
  • 关于属性任何“android:”
  • 您在所有android: 上都有错误?你能显示你的grable 吗?
  • 好的,只有清单中的“@string/google_maps_key”有错误
  • 你的@string/google_maps_key在哪里?目录 ?文件?
【解决方案2】:

检查这两点

  1. 我认为 API 密钥的字符串标签应该包含在资源标签内。

2.你确定你的键值是正确的吗?谷歌地图键通常不以“!”开头标记。 你提到这是你的关键 !AIzaSyBIR8JqMnTpCaul4oX1HzgcR-HoJVR85CA

不带感叹号试试

【讨论】:

  • 你有权利,但你可以看到,在 log cat 中,API 密钥是your API key
  • 在 values 目录和根标签中创建一个新的 xml,因为它应该是资源。在资源里面你可以声明你的字符串
  • 是的,但是在 Google Maps API Android 的文档中,说 When the build is finished, Android Studio opens the google_maps_api.xml and the MapsActivity.java 其中 google_maps_api.xml 包含 google_maps_key
  • 好的,所以请检查 google_maps_api 是否具有我上面提到的结构以及您的 api 键值是否存在
  • 密钥现在是正确的,我注意到的资源标签也是正确的,但是新的错误很奇怪@shubham
猜你喜欢
  • 2016-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 2016-11-24
  • 2015-06-16
相关资源
最近更新 更多