04-18 12:39:18.789: E/AndroidRuntime(11912): FATAL EXCEPTION: main
04-18 12:39:18.789: E/AndroidRuntime(11912): Process: com.example.mapexample, PID: 11912
04-18 12:39:18.789: E/AndroidRuntime(11912): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapexample/com.example.mapexample.MainActivity}: java.lang.NullPointerException
04-18 12:39:18.789: E/AndroidRuntime(11912): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
04-18 12:39:18.789: E/AndroidRuntime(11912): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
04-18 12:39:18.789: E/AndroidRuntime(11912): at android.app.ActivityThread.access$800(ActivityThread.java:139)
04-18 12:39:18.789: E/AndroidRuntime(11912): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
04-18 12:39:18.789: E/AndroidRuntime(11912): at android.os.Handler.dispatchMessage(Handler.java:102)
04-18 12:39:18.789: E/AndroidRuntime(11912): at android.os.Looper.loop(Looper.java:136)
04-18 12:39:18.789: E/AndroidRuntime(11912): at android.app.ActivityThread.main(ActivityThread.java:5105)
04-18 12:39:18.789: E/AndroidRuntime(11912): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 12:39:18.789: E/AndroidRuntime(11912): at java.lang.reflect.Method.invoke(Method.java:515)
04-18 12:39:18.789: E/AndroidRuntime(11912): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
04-18 12:39:18.789: E/AndroidRuntime(11912): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
04-18 12:39:18.789: E/AndroidRuntime(11912): at dalvik.system.NativeStart.main(Native Method)
04-18 12:39:18.789: E/AndroidRuntime(11912): Caused by: java.lang.NullPointerException
04-18 12:39:18.789: E/AndroidRuntime(11912): at com.example.mapexample.MainActivity.onCreate(MainActivity.java:40)
04-18 12:39:18.789: E/AndroidRuntime(11912): at android.app.Activity.performCreate(Activity.java:5275)
04-18 12:39:18.789: E/AndroidRuntime(11912): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-18 12:39:18.789: E/AndroidRuntime(11912): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
04-18 12:39:18.789: E/AndroidRuntime(11912): ... 11 more
package com.example.mapexample;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import android.support.v4.app.FragmentActivity;
//import com.google.android.gms.maps.CameraUpdate;
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.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity {
private GoogleMap googleHarita;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (googleHarita == null) {
googleHarita = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.haritafragment))
.getMap();
if (googleHarita != null) {
googleHarita.setMyLocationEnabled(true);
LocationManager locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);
Criteria criteria=new Criteria();
String provider =locationManager.getBestProvider(criteria, true);
Location mylocation=locationManager.getLastKnownLocation(provider);
double latitude=mylocation.getLatitude();
double longitude=mylocation.getLongitude();
LatLng latLng=new LatLng(latitude,longitude);
googleHarita.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleHarita.animateCamera(CameraUpdateFactory.zoomTo(20));
googleHarita.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("you hereeee"));
double mesafe=distFrom(latitude, longitude, 41.021161, 29.004065);
TextView txt=(TextView)findViewById(R.id.textView1);
txt.setText(String.valueOf(mesafe));
}
}
}
public static double distFrom (double lat1, double lng1, double lat2, double lng2)
{
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
int meterConversion = 1609;
return Double.valueOf((double)dist * meterConversion);
}
@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;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<!-- Harita verisini indirebilmek için izinler -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- Cihazdan Google Servislerine erişmek için izin -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- İndirilenlerin tutulması için izinler -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- kullanıcının konumuna erişmek için gerekli izin -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- OpenGL ES 2.0 Google Maps Android API v2 için gerekiyor -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- Google Maps için API anahtarı -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDm24SBhaRLJGc67smkOmUNBys_JNzDuOk" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.example.mapexample.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>
</application>
</manifest>
答案 0 :(得分:0)
我认为问题在于:
Location mylocation=locationManager.getLastKnownLocation(provider);
请注意,getLastKnownLocation
可以返回null
,这就是NPE
的原因。这是一个修复:
Location mylocation=locationManager.getLastKnownLocation(provider);
double latitude;
double longitude;
if (mylocation != null){
latitude=mylocation.getLatitude();
longitude=mylocation.getLongitude();
}
答案 1 :(得分:0)
似乎mylocation的值为null。
您应该对mylocation进行空验证检查。
if (mylocation != null){
latitude=mylocation.getLatitude();
longitude=mylocation.getLongitude();
}
答案 2 :(得分:0)
首先,您必须检查位置管理器是否已启用,否则该位置将为空。
尝试检查
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
如果您遇到困难Click Here