有人可以帮我解决这个android位置错误吗?

时间:2015-02-07 12:58:41

标签: java android nullpointerexception android-studio google-maps-api-2

您好我m new to programming and the android platform and I正在使用Google Maps Api v2开发应用程序和使用Android Studio 1.02定位服务。基本上当有人启动应用程序时,它会立即放大它s users current location and i m遇到一些奇怪的关于这个的问题。并且很奇怪我的意思是它工作了多次我在我的设备上,但是在2天之后(当我在城外时)我再次运行它,应用程序在启动时崩溃,这个错误apeared:

有没有办法解决这个问题?

另外,你如何设法在android studio 1.02中放置一个actionBar,因为我无法想办法。

Logcat:

  

02-07 14:15:12.350 21921-21921 / com.nanocat.viapp E / AndroidRuntime:   致命异议:主要       过程:com.nanocat.viapp,PID:21921       java.lang.RuntimeException:无法启动活动ComponentInfo {com.nanocat.viapp / com.nanocat.viapp.MapsActivity}:   显示java.lang.NullPointerException               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)               在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)               在android.app.ActivityThread.access $ 900(ActivityThread.java:161)               在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1265)               在android.os.Handler.dispatchMessage(Handler.java:102)               在android.os.Looper.loop(Looper.java:157)               在android.app.ActivityThread.main(ActivityThread.java:5356)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:515)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1265)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)               at dalvik.system.NativeStart.main(Native Method)        引起:java.lang.NullPointerException               在com.nanocat.viapp.MapsActivity.setUpMap(MapsActivity.java:56)               at com.nanocat.viapp.MapsActivity.setUpMapIfNeeded(MapsActivity.java:46)               在com.nanocat.viapp.MapsActivity.onCreate(MapsActivity.java:28)               在android.app.Activity.performCreate(Activity.java:5426)               在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)   在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)   在android.app.ActivityThread.access $ 900(ActivityThread.java:161)   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1265)   在android.os.Handler.dispatchMessage(Handler.java:102)   在android.os.Looper.loop(Looper.java:157)   在android.app.ActivityThread.main(ActivityThread.java:5356)   at java.lang.reflect.Method.invokeNative(Native Method)   在java.lang.reflect.Method.invoke(Method.java:515)   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1265)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)   在dalvik.system.NativeStart.main(本地方法)

我的MapsActivity课程:

package com.nanocat.viapp;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;


public class MapsActivity extends FragmentActivity {


    private GoogleMap mMap;
    double latitude;
    double longitude;

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

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


    private void setUpMapIfNeeded() {
        if (mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    public void setUpMap() {
        Criteria criteria = new Criteria();
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        String provider = locationManager.getBestProvider(criteria, true);
        Location myLocation = locationManager.getLastKnownLocation(provider);
        latitude = myLocation.getLatitude();
        longitude = myLocation.getLongitude();
        LatLng latLng = new LatLng(latitude, longitude);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        CameraPosition cameraPosition = new CameraPosition.Builder()
                  .target(latLng)
                  .tilt(60)
                  .zoom(17)
                  .build();
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

    }
}

我的Manifest.xml:

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

    <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" >
        <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=".MapsActivity"
            android:label="@string/title_activity_maps" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

我的布局:

<fragment
    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"
    android:id="@+id/map"
    tools:context=".MapsActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

1 个答案:

答案 0 :(得分:0)

问题是由于

Location myLocation = locationManager.getLastKnownLocation(provider);
如果尚未获取位置,则

getLastKnowLocation返回null。所以你至少应该检查null Location对象