我正在尝试获取当前位置(纬度和经度)并且我的应用未运行。我有一个TextView来查看longtitude(测试开始)。 这是我的mainActivity代码:
import android.content.Intent;
import android.location.GpsStatus;
import android.location.Location;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.internal.widget.ViewUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
//public Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView)findViewById(R.id.textView);
//creating new object of the method
MyLocationListener myLocationListener = new MyLocationListener();
double longt=myLocationListener.getLocation().getLongitude();
String longi =String.valueOf(longt);
//String lat1 =String.valueOf(lat);
tv.setText(longi);
//myLocationListener.onProviderEnabled(WIFI_SERVICE);
//myLocationListener.onProviderDisabled(WIFI_SERVICE);
// myLocationListener.getLatitude(location);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void process(View view){
Intent intent;
intent = new Intent(this,MapsActivity.class);
startActivity(intent);
}
}
出现了这些错误:
>
8585-8585/com.example.maxi.try_v2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.maxi.try_v2, PID: 8585
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maxi.try_v2/com.example.maxi.try_v2.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.maxi.try_v2.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
如果你能提供帮助,我会很高兴。
这是MyLocationListener类(对不完整的信息感到抱歉)
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
/**
* Created by maxi on 7/17/15.
*/
public class MyLocationListener implements LocationListener {
private Context mContext;
//gps status flag
boolean isGpsEnabled = false;
boolean canGetLocation = false;
//flag for network status
boolean isNetworkEnabled;
//min distance to change updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
//min time between updates in miliseconds
private static final long MIN_TIME_BW_UPDATES = 60000; //1min
protected LocationManager locationManager;
public static double latitude;
public static double longtitude;
Location location;
/* public MyLocationListener(Context context){
this.mContext = context;
getLocation();
}*/
public void onLocationChanged(Location loc) {
latitude = loc.getLatitude();
longtitude = loc.getLongitude();
}
public void getLatitude(Location loc) {
latitude = loc.getLatitude();
String lat = String.valueOf(latitude);
Log.w("poutsa", lat);
}
public void onProviderDisabled(String provider) {
Log.w("myApp", "no network");
}
public void onProviderEnabled(String provider) {
Log.w("myApp", "NetIsOn");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
//http://developer.android.com/reference/android/location/LocationListener
// .html#onStatusChanged%28java.lang.String,%20int,%20android.os.Bundle%29
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
//gettin GPS status
isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//gettin network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGpsEnabled && !isNetworkEnabled) {
//no network
} else {
this.canGetLocation = true;
//first get location from provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longtitude = location.getLongitude();
}
}
}
if (isGpsEnabled){
if (location == null){
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Gps Enabled","Gps Enabled");
if (locationManager != null){
location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
if (location != null){
latitude = location.getLatitude();
longtitude = location.getLongitude();
}
}
}
}
}
}catch (Exception e){
e.printStackTrace();
}
return location;
}
}
答案 0 :(得分:0)
引起:java.lang.NullPointerException 在com.example.maxi.try_v2.MainActivity.onCreate(MainActivity.java:27)
打开 MainActivity.java ,查看第27行:您有一个null
变量。
既然你没有告诉第27行是什么,我就不能再帮忙了。
只需检查null
,例如:代码中的myLocationListener.getLocation()
可能是null
。
答案 1 :(得分:0)
这会给你一个NullPointerException
,这意味着你在向位置发出请求时会得到空
double longt=myLocationListener.getLocation().getLongitude();