谷歌地图错误,由元数据引起

时间:2016-03-25 13:25:16

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

我在尝试使用标签页中实现的谷歌地图制作标签式布局时出错了

我收到以下错误

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,它给出了一个错误,指出 &#34;必须声明元素字符串&#34;

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

这一切最初都是在我注意到操作栏被删除并尝试将其更改为主要活动中的工具栏时启动的,因为这样可以让我从原来的错误中运行,这是因为需要停止调用windowactionbar < / p>

主要活动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的新手,现在不再理解,可以做些什么来让它运行而不会因错误而崩溃。

摇篮

   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'

}

2 个答案:

答案 0 :(得分:0)

尝试改变:

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

答案 1 :(得分:0)

检查这两点

  1. API密钥的字符串标记应该包含在我认为的资源标记内。
  2. 2.您确定您的关键值是否合适? Google地图密钥通常不以“!”开头标记。 你提到这是你的关键 !AIzaSyBIR8JqMnTpCaul4oX1HzgcR-HoJVR85CA

    尝试没有感叹号