在Android Studio中显示地图页面时出错

时间:2015-10-02 07:45:34

标签: android maps

我很难为我的谷歌地图页面调试主java类。每当我运行我的应用程序时,我总是得到一个无响应输出。感谢那些愿意回答的人。

以下是地图的java代码:

                  public class MapsActivity extends FragmentActivity {

                private GoogleMap map;

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

                @Override
                protected void onResume() {
                    super.onResume();
                    setUpMapIfNeeded();
                }

                public void onSearch(View view) {
                    EditText location_tf = (EditText) findViewById(R.id.TFPickup);
                    String location = location_tf.getText().toString();

                    List<Address> addressList = null; //stores the list of address
                    if (location != null || !location.equals("")) {
                        Geocoder geocoder = new Geocoder(this);
                        try {
                            addressList = geocoder.getFromLocationName(location, 1);


                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        Address address = addressList.get(0); //stores lang and long
                        LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
                        map.addMarker(new MarkerOptions().position(latLng).title("You are here!"));
                        map.animateCamera(CameraUpdateFactory.newLatLng(latLng));

                    }
                }


                private void setUpMapIfNeeded() {

                    if (map == null) {
                        // Try to obtain the map from the SupportMapFragment.
                        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                                .getMap(); //creates map fragment //getmap- initializes map and view
                        // Check if we were successful in obtaining the map.
                        if (map != null) {
                            setUpMap();
                        }
                    }
                }


                private void setUpMap() {



                    map.addMarker(new MarkerOptions().position(new LatLng(0,0)).title("You are here!"));
                    map.setMyLocationEnabled(true); //see current location
                }


            }

以下是地图的xml文件:

                <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:backgroundTint="@android:color/background_dark"
                android:weightSum="1">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/bg"
                    />


                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/header"
                    android:focusable="false"
                    android:id="@+id/header1"
                    android:layout_above="@+id/logo" />

                <ImageView
                    android:layout_width="275dp"
                    android:layout_height="220dp"
                    android:src="@drawable/logo"
                    android:id="@+id/logo"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true" />


                <EditText
                    android:layout_width="300dp"
                    android:layout_height="wrap_content"
                    android:text="Location"
                    android:textColor="@android:color/white"
                    android:textStyle="italic"
                    android:id="@+id/TFPickup"
                    android:layout_alignBottom="@+id/logo"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <Button
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="GO"
                    android:id="@+id/Bsearch"
                    android:layout_gravity="right"
                    android:onClick="onSearch"
                    android:layout_alignBottom="@+id/TFPickup"
                    android:layout_toRightOf="@+id/TFPickup"
                    android:layout_toEndOf="@+id/TFPickup" />

                    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                        xmlns:tools="http://schemas.android.com/tools"
                        android:layout_width="385dp"
                        android:layout_height="260dp"
                        android:id="@+id/map"
                        tools:context=".MapsActivity"
                        android:name="com.google.android.gms.maps.SupportMapFragment"

                        android:layout_below="@+id/logo"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true" />

                <ImageButton
                    android:layout_width="140dp"
                    android:layout_height="40dp"
                    android:src="@drawable/book_now"
                    android:id="@+id/book_now"
                    android:layout_below="@+id/map"
                    android:layout_centerHorizontal="true"
                    android:onClick="onClick1"/>

                    <SearchView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Location"
                        android:layout_alignTop="@+id/map"
                        android:layout_centerHorizontal="true"
                        android:id="@+id/search"
                        />


                </RelativeLayout>



            And here is the function to call the map activity page:

                 case R.id.bBook:
                            userLocalStore.getLoggedInUser();
                            userLocalStore.setUserLoggedIn(true);
                           startActivity(new Intent(this, MapsActivity.class));
                          break;

这是请求的清单文件:

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

            <uses-permission android:name="android.permission.INTERNET" />
                <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:theme="@style/Theme.AppCompat.NoActionBar" >
                    <meta-data
                        android:name="com.google.android.gms.version"
                        android:value="@integer/google_play_services_version">

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

                    </meta-data>
                    <activity
                        android:name=".SplashScreen"
                        android:label="@string/app_name" >
                        <intent-filter>
                            <action android:name="android.intent.action.MAIN" />

                            <category android:name="android.intent.category.LAUNCHER" />
                        </intent-filter>
                    </activity>
                    <activity
                        android:name=".Login"
                        android:label="@string/title_activity_login" >
                    </activity>
                    <activity
                        android:name=".Register"
                        android:label="@string/title_activity_register" >
                    </activity>
                    <activity android:name=".MainActivity"
                        android:label="Main"></activity>

                <activity android:name=".MapsActivity"
                    android:label="MapsActivity">

                </activity>
                </application>

            </manifest>

这是请求的Logcat:  每当我从登录页面点击“立即预订”按钮时,都会生成此结果。

     10-02 15:57:16.864  24679-24679/? D/dalvikvm﹕ Late-enabling CheckJNI
     10-02 15:57:17.354  24679-24679/com.example.alienwarevin.loginregister         D/ActivityThread﹕ setTargetHeapUtilization:0.25

    10-02 15:57:17.354  24679-24679/com.example.alienwarevin.loginregister D/ActivityThread﹕ setTargetHeapIdealFree:8388608
    10-02 15:57:17.354  24679-24679/com.example.alienwarevin.loginregister D/ActivityThread﹕ setTargetHeapConcurrentStart:2097152
    10-02 15:57:18.544  24679-24679/com.example.alienwarevin.loginregister D/dalvikvm﹕ GC_FOR_ALLOC freed 110K, 14% free 13109K/15107K, paused 17ms, total 17ms

    10-02 15:57:18.574  24679-24679/com.example.alienwarevin.loginregister I/dalvikvm-heap﹕ Grow heap (frag case) to 29.374MB for 8294416-byte allocation
    10-02 15:57:18.624  24679-24681/com.example.alienwarevin.loginregister D/dalvikvm﹕ GC_CONCURRENT freed 1K, 9% free 21208K/23239K, paused 15ms+6ms, total 52ms
    10-02 15:57:19.654  24679-24679/com.example.alienwarevin.loginregister D/libEGL﹕ loaded /system/lib/egl/libEGL_adreno200.so
    10-02 15:57:19.694  24679-24679/com.example.alienwarevin.loginregister D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_adreno200.so
    10-02 15:57:19.704  24679-24679/com.example.alienwarevin.loginregister D/libEGL﹕ loaded /system/lib/egl/libGLESv2_adreno200.so
    10-02 15:57:19.714  24679-24679/com.example.alienwarevin.loginregister I/Adreno200-EGL﹕ <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107_msm8625_JB_REL_2.0.3_CL3357771_release_AU (CL3357771)


    Build Date: 02/25/13 Mon
                Local Branch:
                Remote Branch: quic/jb_rel_2.0.3
                Local Patches: NONE
                Reconstruct Branch: AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107 +  NOTHING
            10-02 15:57:20.484  24679-24679/com.example.alienwarevin.loginregister D/OpenGLRenderer﹕ Enabling debug mode 0
            10-02 15:57:22.514  24679-24681/com.example.alienwarevin.loginregister D/dalvikvm﹕ GC_CONCURRENT freed 3659K, 20% free 23707K/29319K, paused 12ms+3ms, total 88ms
            10-02 15:57:22.614  24679-24679/com.example.alienwarevin.loginregister I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
            10-02 15:57:22.624  24679-24679/com.example.alienwarevin.loginregister W/dalvikvm﹕ VFY: unable to resolve virtual method 577: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
            10-02 15:57:22.624  24679-24679/com.example.alienwarevin.loginregister D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002


    10-02 15:57:22.624  24679-24679/com.example.alienwarevin.loginregister I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
            10-02 15:57:22.624  24679-24679/com.example.alienwarevin.loginregister W/dalvikvm﹕ VFY: unable to resolve virtual method 579: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
            10-02 15:57:22.624  24679-24679/com.example.alienwarevin.loginregister D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
            10-02 15:57:22.674  24679-24679/com.example.alienwarevin.loginregister I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
            10-02 15:57:22.674  24679-24679/com.example.alienwarevin.loginregister W/dalvikvm﹕ VFY: unable to resolve virtual method 614: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
            10-02 15:57:22.674  24679-24679/com.example.alienwarevin.loginregister D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002


     10-02 15:57:22.674  24679-24679/com.example.alienwarevin.loginregister I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
            10-02 15:57:22.674  24679-24679/com.example.alienwarevin.loginregister W/dalvikvm﹕ VFY: unable to resolve virtual method 636: Landroid/content/res/TypedArray;.getType (I)I
            10-02 15:57:22.674  24679-24679/com.example.alienwarevin.loginregister D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
            10-02 15:57:23.224  24679-24679/com.example.alienwarevin.loginregister E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
            10-02 15:57:23.224  24679-24679/com.example.alienwarevin.loginregister E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
            10-02 15:57:33.574  24679-24679/com.example.alienwarevin.loginregister I/dalvikvm﹕ Could not find method android.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
            10-02 15:57:33.574  24679-24679/com.example.alienwarevin.loginregister W/dalvikvm﹕ VFY: unable to resolve virtual method 243: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
            10-02 15:57:33.574  24679-24679/com.example.alienwarevin.loginregister D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x00c2
            10-02 15:57:33.574  24679-24679/com.example.alienwarevin.loginregister I/dalvikvm﹕ DexOpt: access denied from Lcom/google/android/gms/common/GooglePlayServicesUtil; to field Landroid/app/Notification;.extras
            10-02 15:57:33.574  24679-24679/com.example.alienwarevin.loginregister W/dalvikvm﹕ VFY: unable to resolve instance field 18
            10-02 15:57:33.574  24679-24679/com.example.alienwarevin.loginregister D/dalvikvm﹕ VFY: replacing opcode 0x54 at 0x00e1
            10-02 15:57:33.574  24679-24679/com.example.alienwarevin.loginregister E/dalvikvm﹕ Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
            10-02 15:57:33.574  24679-24679/com.example.alienwarevin.loginregister W/dalvikvm﹕ VFY: unable to resolve check-cast 25 (Landroid/app/AppOpsManager;) in Lcom/google/android/gms/common/GooglePlayServicesUtil;
            10-02 15:57:33.574  24679-24679/com.example.alienwarevin.loginregister D/dalvikvm﹕ VFY: replacing opcode 0x1f at 0x000e
            10-02 15:57:33.574  24679-24679/com.example.alienwarevin.loginregister I/dalvikvm﹕ Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzh


      10-02 15:57:33.574  24679-24679/com.example.alienwarevin.loginregister W/dalvikvm﹕ VFY: unable to resolve virtual method 539: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
            10-02 15:57:33.574  24679-24679/com.example.alienwarevin.loginregister D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000b
            10-02 15:57:33.594  24679-24679/com.example.alienwarevin.loginregister I/zzy﹕ Making Creator dynamically
            10-02 15:57:33.774  24679-24679/com.example.alienwarevin.loginregister E/dalvikvm﹕ Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.kz.a
            10-02 15:57:33.774  24679-24679/com.example.alienwarevin.loginregister W/dalvikvm﹕ VFY: unable to resolve check-cast 36 (Landroid/app/AppOpsManager;) in Lcom/google/android/gms/common/kz;
            10-02 15:57:33.774  24679-24679/com.example.alienwarevin.loginregister D/dalvikvm﹕ VFY: replacing opcode 0x1f at 0x0010
            10-02 15:57:33.774  24679-24679/com.example.alienwarevin.loginregister I/dalvikvm﹕ Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.kz.a
            10-02 15:57:33.774  24679-24679/com.example.alienwarevin.loginregister W/dalvikvm﹕ VFY: unable to resolve virtual method 446: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
            10-02 15:57:33.774  24679-24679/com.example.alienwarevin.loginregister D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000d
            10-02 15:57:33.784  24679-24679/com.example.alienwarevin.loginregister I/dalvikvm﹕ Could not find method android.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.kz.b
            10-02 15:57:33.784  24679-24679/com.example.alienwarevin.loginregister W/dalvikvm﹕ VFY: unable to resolve virtual method 155: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
            10-02 15:57:33.784  24679-

    24679/com.example.alienwarevin.loginregister D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0206
                10-02 15:57:33.784  24679-24679/com.example.alienwarevin.loginregister I/dalvikvm﹕ DexOpt: access denied from Lcom/google/android/gms/common/kz; to field Landroid/app/Notification;.extras
                10-02 15:57:33.784  24679-24679/com.example.alienwarevin.loginregister W/dalvikvm﹕ VFY: unable to resolve instance field 27
                10-02 15:57:33.784  24679-24679/com.example.alienwarevin.loginregister D/dalvikvm﹕ VFY: replacing opcode 0x54 at 0x0225
                10-02 15:57:33.784  24679-24679/com.example.alienwarevin.loginregister E/dalvikvm﹕ Could not find class 'android.os.UserManager', referenced from method com.google.android.gms.common.kz.j
                10-02 15:57:33.784  24679-24679/com.example.alienwarevin.loginregister W/dalvikvm﹕ VFY: unable to resolve check-cast 245 (Landroid/os/UserManager;) in Lcom/google/android/gms/common/kz;
                10-02 15:57:33.784  24679-24679/com.example.alienwarevin.loginregister D/dalvikvm﹕ VFY: replacing opcode 0x1f at 0x000e
                10-02 15:57:33.804  24679-24679/com.example.alienwarevin.loginregister D/ChimeraCfgMgr﹕ Loading module com.google.android.gms.maps from APK /data/data/com.google.android.gms/app_chimera/chimera-module-root/module-d6493fd1739bd078c31af6d7054443fe0541820e/MapsModule.apk
                10-02 15:57:33.804  24679-24679/com.example.alienwarevin.loginregister D/ChimeraModuleLdr﹕ Loading module APK /data/data/com.google.android.gms/app_chimera/chimera-module-root/module-d6493fd1739bd078c31af6d7054443fe0541820e/MapsModule.apk
                10-02 15:57:33.864  24679-24679/com.example.alienwarevin.loginregister D/ChimeraFileApk﹕ Primary ABI of requesting process is armeabi-v7a
                10-02 15:57:33.864  24679-24679/com.example.alienwarevin.loginregister D/ChimeraFileApk﹕ Classloading successful, but code may not be optimized. It will either run in fallback (interpreted mode) or the odex has been found and isDexOptNeeded is misreporting a failure.
                10-02 15:57:34.004  24679-24679/com.example.alienwarevin.loginregister I/Google Maps Android API﹕ Google Play services client version: 7895000
                10-02 15:57:34.004  24679-24679/com.example.alienwarevin.loginregister I/Google Maps Android API﹕ Google Play services package version: 8115034
                10-02 15:57:34.014  24679-24679/com.example.alienwarevin.loginregister D/AndroidRuntime﹕ Shutting down VM
                10-02 15:57:34.014  24679-24679/com.example.alienwarevin.loginregister W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4135a438)
                10-02 15:57:34.044  24679-24679/com.example.alienwarevin.loginregister E/AndroidRuntime﹕ FATAL EXCEPTION: main
                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alienwarevin.loginregister/com.example.alienwarevin.loginregister.MapsActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
                            at android.app.ActivityThread.access$700(ActivityThread.java:143)
                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
                            at android.os.Handler.dispatchMessage(Handler.java:99)
                            at android.os.Looper.loop(Looper.java:137)
                            at android.app.ActivityThread.main(ActivityThread.java:4960)
                            at java.lang.reflect.Method.invokeNative(Native Method)
                            at java.lang.reflect.Method.invoke(Method.java:511)
                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
                            at dalvik.system.NativeStart.main(Native Method)
                     Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
                            at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
                            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:318)
                            at android.app.Activity.setContentView(Activity.java:1925)
                            at com.example.alienwarevin.loginregister.MapsActivity.onCreate(MapsActivity.java:27)
                            at android.app.Activity.performCreate(Activity.java:5203)
                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)

                            at android.app.ActivityThread.access$700(ActivityThread.java:143)
                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
                            at android.os.Handler.dispatchMessage(Handler.java:99)
                            at android.os.Looper.loop(Looper.java:137)
                            at android.app.ActivityThread.main(ActivityThread.java:4960)
                            at java.lang.reflect.Method.invokeNative(Native Method)
                            at java.lang.reflect.Method.invoke(Method.java:511)
                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
                            at dalvik.system.NativeStart.main(Native Method)
                 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
                        at com.google.maps.api.android.lib6.c.ad.a(Unknown Source)
                        at com.google.maps.api.android.lib6.a.e.a(Unknown Source)


These results whenever I hit the Book now button in the Login page.        

1 个答案:

答案 0 :(得分:0)

Geocoder geocoder = new Geocoder(this);

应该是

Geocoder geocoder = new Geocoder(getActivity());