Why i'm getting a white screen in map app

2019-01-12 05:24发布

问题:

I'm new here and also new in programming. I'm making map, I have followed a lot of tutorials, all of them very simillar but I don't know why my app doesn't work. The app doesn't crash, but when I open the map appears the app title and a white screen where the map should appear. Do you know why is this happening?

Here are my app manifest, the activity:main.xml and the mainactivity.java, maybe here's the error that I can't find.

Also if helps logcat says me: Error opening trace file: No such file or directory (2)

APP MANIFEST

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tdr.mapa1"
    android:versionCode="1"
    android:versionName="1.0" >

    <permission
      android:name="com.tdr.mapa1.permission.MAPS_RECEIVE"
      android:protectionLevel="signature"/>

<uses-permission android:name="permission_name"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.tdr.mapa1.permission.MAPS_RECEIVE"/>
<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_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

   <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <uses-sdk
        android:minSdkVersion="12" 
        android:targetSdkVersion="17" />
    <application

        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

    <uses-library android:name="com.google.android.maps" 
                  android:required="false"/>

    <activity
        android:name="com.tdr.mapa1.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>
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="<API_KEY>"/>

    </application>

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

    </manifest>

MAIN ACTIVITY

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

</RelativeLayout>

MAINACTIVITY.JAVA

package com.tdr.mapa1;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

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



    }
}

Hope you can help me.

回答1:

First your error:

Error opening trace file: No such file or directory (2)

doesn't mean anything and you should node pay any attention to it.

Second, both meta-data tags should be inside of the application scope, like so:

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

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

Lastly, you are not actually creating any map object in your activity layout file, what you have there is an empty RelativeLayout and hence the empty white screen. What you need is to add an MapFragment in there or SupportMapFramgnet depending on your target api. (In your case it would be MapFragment.)

Like so:

<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:name="com.google.android.gms.maps.MapFragment"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

You can get more detailed information from this blog post I wrote on this topic:

Google Maps API V2 Guide



回答2:

See here,just change the api key with your key in manifest file and follow these steps: and make sure that generate api key with package name which is mentioned in android manifest file and your google_play_services_lib project should be present in your project's work space only.

Manifest file:

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

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <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_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.geeklabs.map.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>

        <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="replace with your API key"/>

    </application>

</manifest>

MainActivity.java:

    package com.geeklabs.map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

}

activity_main.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="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

And make sure following steps done correct or not:

Steps: * to ensure that device has Google Play services APK * to install Google Play Service rev. more than 2

  • to create project at https://code.google.com/apis/console/
  • to enable "Google Maps Android API v2"
  • to register of SHA1 in project (NOW, YOU NEED WRITE SHA1;your.app.package.name) at APIs console and get API KEY
  • to copy directory ANDROID_SDK_DIR/extras/google/google_play_services/libproject/google-play-services_lib to root of your project
  • to add next line to the YOUR_PROJECT/project.properties

android.library.reference.1=google-play-services_lib

  • to add next lines to the YOUR_PROJECT/proguard-project.txt

-keep class * extends java.util.ListResourceBundle {

protected Object[][] getContents();

}

Okay, now you ready to create your own Google Map app with using Google Map APIs V2 for Android.

If you create application with min SDK = 8, please use android support library v4 + SupportMapFragment instead of MapFragment.

After got this let me know.