我正试图启动我的应用程序,但该代码崩溃使它难以解决。 E / UncaughtException:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' 在com.websmeet.vroumchat.activity.NearbyHomeActivity.setLocationData(NearbyHomeActivity.java:543)
public static DatabaseReference profileRef;
public static void setLocationData(Context context) {
if (context != null) {
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser != null) {
profileRef = FirebaseDatabase.getInstance().getReference("users").child(firebaseUser.getUid());
}
profileRef.child("latitude").setValue(VroumchatApplication.getInstance().getLat());
profileRef.child("longitude").setValue(VroumchatApplication.getInstance().getLng());
profileRef.child("location").setValue(VroumchatApplication.getInstance().getLat() + "," + VroumchatApplication.getInstance().getLng());
profileRef.child("address").setValue(VroumchatApplication.getInstance().getArea());
profileRef.child("city").setValue(VroumchatApplication.getInstance().getCity());
profileRef.child("country").setValue(VroumchatApplication.getInstance().getCountry());
}
}
这是我的logcat,我启动它时他对这个错误的评价
01-02 02:24:53.123 2026-2026/? E/PagedView: updateCurrentPageScroll: childCount: 2currentPage0newX: 0
01-02 02:24:54.601 1785-12127/? E/AudioTrack: did not receive expected priority boost on time
01-02 02:25:00.365 12156-12156/? E/SQLiteLog: (283) recovered 42 frames from WAL file /data/data/com.android.providers.media/databases/external.db-wal
01-02 02:25:08.546 2026-2026/? E/PagedView: updateCurrentPageScroll: childCount: 2currentPage0newX: 0
01-02 02:25:15.728 12239-12239/? E/memtrack: Couldn't load memtrack module (No such file or directory)
01-02 02:25:15.728 12239-12239/? E/android.os.Debug: failed to load memtrack module: -2
01-02 02:25:27.071 2026-2026/? E/PagedView: getScrollProgress: page: 0 : leftX: 0 delta: 0 scrollProgress : 0.0halfScreenSize360screenCenter360
01-02 02:25:27.071 2026-2026/? E/PagedView: getScrollProgress: page: 1 : leftX: 720 delta: -720 scrollProgress : -1.0halfScreenSize360screenCenter360
01-02 02:25:29.580 12293-12293/? E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.websmeet.vroumchat.activity.NearbyHomeActivity.setLocationData(NearbyHomeActivity.java:543)
at com.websmeet.vroumchat.utility.GPSTracker.onLocationChanged(GPSTracker.java:219)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:281)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:210)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:226)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
01-02 02:25:29.711 1785-12292/? E/AudioTrack: did not receive expected priority boost on time
01-02 02:25:29.790 12293-12293/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.websmeet.vroumchat, PID: 12293
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.websmeet.vroumchat.activity.NearbyHomeActivity.setLocationData(NearbyHomeActivity.java:543)
at com.websmeet.vroumchat.utility.GPSTracker.onLocationChanged(GPSTracker.java:219)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:281)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:210)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:226)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
01-02 02:25:30.273 12293-12356/? E/FirebaseCrash: Unable to parse Json response string to get message: No value for crashes
01-02 02:25:33.348 1785-12364/? E/AudioTrack: did not receive expected priority boost on time
01-02 02:25:33.934 12365-12365/? E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.websmeet.vroumchat.activity.NearbyHomeActivity.setLocationData(NearbyHomeActivity.java:543)
at com.websmeet.vroumchat.utility.GPSTracker.onLocationChanged(GPSTracker.java:219)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:281)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:210)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:226)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
01-02 02:25:34.144 12365-12365/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.websmeet.vroumchat, PID: 12365
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.websmeet.vroumchat.activity.NearbyHomeActivity.setLocationData(NearbyHomeActivity.java:543)
at com.websmeet.vroumchat.utility.GPSTracker.onLocationChanged(GPSTracker.java:219)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:281)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:210)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:226)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
01-02 02:25:34.285 12365-12449/? E/FirebaseCrash: Unable to parse Json response string to get message: No value for crashes
01-02 02:26:02.930 12365-12389/? E/FirebaseInstanceId: Token retrieval failed: SERVICE_NOT_AVAILABLE
0
我试图插入try {} else {},但再次无法正常工作
public static void setLocationData(Context context) {
if(context!=null){
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
try {
if (firebaseUser != null) {
profileRef = FirebaseDatabase.getInstance().getReference("users").child(firebaseUser.getUid());
} else {
profileRef.child("latitude").setValue(VroumchatApplication.getInstance().getLat());
profileRef.child("longitude").setValue(VroumchatApplication.getInstance().getLng());
profileRef.child("location").setValue(VroumchatApplication.getInstance().getLat() + "," + VroumchatApplication.getInstance().getLng());
profileRef.child("address").setValue(VroumchatApplication.getInstance().getArea());
profileRef.child("city").setValue(VroumchatApplication.getInstance().getCity());
profileRef.child("country").setValue(VroumchatApplication.getInstance().getCountry());
}
}
}
}
问题是启动应用程序时,它尝试在登录前获取位置,但是我想在登录后获取位置。
请提供任何帮助。 谢谢最后我通过编辑GPSTracker.java解决了它
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000; // 1 second
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
if (Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return null;
}
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network 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.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
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();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS() {
if (locationManager != null) {
if (Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return ;
}
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if (location != null) {
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/Wi-Fi enabled
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
((Activity)mContext).finish();
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
this.location = location;
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
答案 0 :(得分:0)
profileRef并未针对其他情况定义,为了获得最佳的错误处理,也可以将try {} catch(Exception e){}包装起来,至少可以轻松找出问题所在。