在Android中的真实设备上运行时,地图不会显示

时间:2013-04-24 09:50:36

标签: android google-maps nullpointerexception

我已编写代码来显示地图并在真实设备上运行。但是Map显示为null。

这是我的代码,

 package com.example.findproperty;


public class MainActivity extends FragmentActivity implements LocationListener {

private static final String TABLE_NAME = "LatsLongs";
private static final String COLUMN_ID = "_Id";
private static final String Latitude = "Latitude";
private static final String Longitude = "Longitude";
Double Lat;
Double Long;
protected static final int UPDATE_LATLNG = 2;
protected static final int UPDATE_ADDRESS = 0;
public final android.content.Context Context = MainActivity.this;
LocationManager lm;
String textmessage = "";
String phonenumber = "";
GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int chkGooglePlayServices = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(Context);
    if (chkGooglePlayServices != ConnectionResult.SUCCESS) {
        GooglePlayServicesUtil.getErrorDialog(chkGooglePlayServices,
                (Activity) Context, 1122).show();
    } else {
        mMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        final boolean isProviderEnabled = lm
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        String locationprovider = lm.getBestProvider(criteria, true);

        // Location Update
        LocationListener listener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                mMap.clear();
                final CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(new LatLng(location.getLatitude(), location
                                .getLongitude())).zoom(14.0f).build();
                final CameraUpdate cameraUpdate = CameraUpdateFactory
                        .newCameraPosition(cameraPosition);
                mMap.moveCamera(cameraUpdate);
            }

            AlertDialog.Builder dialog = new AlertDialog.Builder(Context)
                    .setTitle(R.string.RemindEnableGps);
            AlertDialog alert = dialog.create();

            @Override
            public void onProviderDisabled(String provider) {
                alert.setCanceledOnTouchOutside(false);
                alert.setCancelable(true);
                alert.show();

            }

            @Override
            public void onProviderEnabled(String provider) {
                alert.dismiss();

            }

            @Override
            public void onStatusChanged(String provider, int status,
                    Bundle extras) {
            }
        };

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                listener);
        final Location mLocation = lm
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);

        final CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(mLocation.getLatitude(), mLocation
                        .getLongitude())).zoom(14.0f).build();
        final CameraUpdate cameraUpdate = CameraUpdateFactory
                .newCameraPosition(cameraPosition);
        mMap.moveCamera(cameraUpdate);
    }
}

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

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

}

的Manifest.xml,

<?xml version="1.0" encoding="utf-8"?>

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

<permission
    android:name="com.example.findproperty.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

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

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

</manifest>

activity_main.xml中,

<?xml version="1.0"?>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<fragment
    android:id="@+id/map"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="30dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:layout_marginTop="60dp"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:clickable="true" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/edtSearch"
    android:layout_alignLeft="@+id/map"
    android:text="Limit Distance:" />

<EditText
    android:id="@+id/edtSearch"
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/btnSearch"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:inputType="number" />

<Button
    android:id="@+id/btnSearch"
    android:layout_width="wrap_content"
    android:layout_height="35dp"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/map"
    android:layout_marginRight="14dp"
    android:layout_marginTop="15dp"
    android:text="Find" />

</RelativeLayout>

当我调试它显示的应用程序时,mMap为null并且mLocation也为null。我没有得到Gps坐标。

这是我的Logcat,

04-24 15:17:53.143: I/ApplicationPackageManager(10248): cscCountry is not German : INS
04-24 15:17:53.182: D/dalvikvm(10248): DexOpt: couldn't find field Landroid/content/res/Configuration;.smallestScreenWidthDp
04-24 15:17:53.182: W/dalvikvm(10248): VFY: unable to resolve instance field 23
04-24 15:17:53.182: D/dalvikvm(10248): VFY: replacing opcode 0x52 at 0x0012
04-24 15:17:53.190: D/dalvikvm(10248): VFY: dead code 0x0014-0018 in Lcom/google/android/gms/common/GooglePlayServicesUtil;.b (Landroid/content/res/Resources;)Z
04-24 15:17:53.331: W/dalvikvm(10248): Unable to resolve superclass of Lmaps/p/s; (425)
04-24 15:17:53.331: W/dalvikvm(10248): Link of class 'Lmaps/p/s;' failed
04-24 15:17:53.331: W/dalvikvm(10248): Unable to resolve superclass of Lmaps/y/bo; (3818)
04-24 15:17:53.331: W/dalvikvm(10248): Link of class 'Lmaps/y/bo;' failed
04-24 15:17:53.339: W/dalvikvm(10248): Unable to resolve superclass of Lmaps/i/k; (4206)
04-24 15:17:53.339: W/dalvikvm(10248): Link of class 'Lmaps/i/k;' failed
04-24 15:17:53.339: E/dalvikvm(10248): Could not find class 'maps.i.k', referenced from method maps.z.ag.a
04-24 15:17:53.339: W/dalvikvm(10248): VFY: unable to resolve new-instance 3538 (Lmaps/i/k;) in Lmaps/z/ag;
04-24 15:17:53.339: D/dalvikvm(10248): VFY: replacing opcode 0x22 at 0x006d
04-24 15:17:53.354: D/dalvikvm(10248): VFY: dead code 0x006f-007f in Lmaps/z/ag;.a (Landroid/view/LayoutInflater;Lcom/google/android/gms/maps/GoogleMapOptions;ZLjava/lang/String;)Lmaps/z/ag;
04-24 15:17:53.448: D/dalvikvm(10248): GC_CONCURRENT freed 367K, 48% free 3090K/5831K, external 688K/1036K, paused 3ms+4ms
04-24 15:17:53.472: I/ApplicationPackageManager(10248): cscCountry is not German : INS
04-24 15:17:53.643: D/dalvikvm(10248): GC_CONCURRENT freed 315K, 45% free 3324K/6023K, external 688K/1036K, paused 4ms+5ms
04-24 15:17:53.768: E/Google Maps Android API(10248): Failed to load map.  Could not contact Google servers.
04-24 15:17:53.870: W/dalvikvm(10248): threadid=1: thread exiting with uncaught exception (group=0x40018578)
04-24 15:17:53.870: E/AndroidRuntime(10248): FATAL EXCEPTION: main
04-24 15:17:53.870: E/AndroidRuntime(10248): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.findproperty/com.example.findproperty.MainActivity}: java.lang.NullPointerException
04-24 15:17:53.870: E/AndroidRuntime(10248):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-24 15:17:53.870: E/AndroidRuntime(10248):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-24 15:17:53.870: E/AndroidRuntime(10248):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-24 15:17:53.870: E/AndroidRuntime(10248):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-24 15:17:53.870: E/AndroidRuntime(10248):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 15:17:53.870: E/AndroidRuntime(10248):    at android.os.Looper.loop(Looper.java:130)
04-24 15:17:53.870: E/AndroidRuntime(10248):    at android.app.ActivityThread.main(ActivityThread.java:3687)
04-24 15:17:53.870: E/AndroidRuntime(10248):    at java.lang.reflect.Method.invokeNative(Native Method)
04-24 15:17:53.870: E/AndroidRuntime(10248):    at java.lang.reflect.Method.invoke(Method.java:507)
04-24 15:17:53.870: E/AndroidRuntime(10248):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-24 15:17:53.870: E/AndroidRuntime(10248):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-24 15:17:53.870: E/AndroidRuntime(10248):    at dalvik.system.NativeStart.main(Native Method)
04-24 15:17:53.870: E/AndroidRuntime(10248): Caused by: java.lang.NullPointerException
04-24 15:17:53.870: E/AndroidRuntime(10248):    at com.example.findproperty.MainActivity.onCreate(MainActivity.java:130)
04-24 15:17:53.870: E/AndroidRuntime(10248):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-24 15:17:53.870: E/AndroidRuntime(10248):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-24 15:17:53.870: E/AndroidRuntime(10248):    ... 11 more
04-24 15:17:54.089: D/dalvikvm(10248): GC_CONCURRENT freed 311K, 44% free 3542K/6279K, external 929K/1036K, paused 4ms+4ms

该设备已安装google-play-services。并启用了GPS。 但是地图显示为空。

我的移动设备无法访问互联网。

请有人帮忙!!!

1 个答案:

答案 0 :(得分:0)

  1. 在这部分:

    我认为“My_API_KEY”部分已被您的实际API密钥替换,对吧?

  2. 此行被抛出异常吗?

    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

  3. 看一下这个链接:

  4. Google Play Service - SupportMapFragment.getMap() always returning null