我不知道为什么我的代码总是提供一个null提供程序我尝试了很多解决方案但是如果错误消失它总是有两种情况它将不会添加到数据库中,如果它在应用程序午餐中添加错误。
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, LocationListener {
private TextView userName, userEmail;
private CircleImageView userImageView;
private String TAG = "MainActivity";
Location mLastLocation;
double latitude, longitude;
private PositionData positionData;
private DatabaseReference mFirebaseDatabaseReference;
private String uid;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
this.setTitle("Couverture 4G");
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
uid = user.getUid();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header = navigationView.getHeaderView(0);
// View header = LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);
userName = (TextView) header.findViewById(R.id.userNameDrawer);
userEmail = (TextView) header.findViewById(R.id.userEmailDrawer);
userImageView = (CircleImageView) header.findViewById(R.id.userImageViewDrawer);
// FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// Name, email address, and profile photo Url
String namee = user.getDisplayName();
String emaile = user.getEmail();
Uri photoUrl = user.getPhotoUrl();
// The user's ID, unique to the Firebase project. Do NOT use this value to
// authenticate with your backend server, if you have one. Use
// FirebaseUser.getToken() instead.
String uid = user.getUid();
Log.d(TAG, "uid = " + uid + " name = " + namee + " email = " + emaile);
userEmail.setText(emaile);
userName.setText(namee);
if (photoUrl != null)
Glide.with(this).load(photoUrl).into(userImageView);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
// Getting latitude of the current location
latitude = location.getLatitude();
// Getting longitude of the current location
longitude = location.getLongitude();
// Creating a LatLng object for the current location
Log.d("TAG MAPS", "lat = " + latitude + "long = " + longitude);
this.onLocationChanged(location);
}
FragmentTransaction ftt = getSupportFragmentManager().beginTransaction()
.replace(R.id.main_activity_container, new ClimatFragment()).addToBackStack(null);
ftt.commit();
}
public boolean checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Asking user if explanation is needed
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
return false;
} else {
return true;
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
if (count == 1) {
count = 0;
moveTaskToBack(true);
} else {
Toast.makeText(getApplicationContext(), "Tapez sur le Bouton Retour une seconde fois pour quitter.", Toast.LENGTH_SHORT).show();
count++;
}
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_map) {
// Handle the camera action
startActivity(new Intent(MainActivity.this, MapActivity.class));
} else if (id == R.id.nav_speedtest) {
FragmentTransaction ftt = getSupportFragmentManager().beginTransaction()
.replace(R.id.main_activity_container, new SpeedTestFragment()).addToBackStack(null);
ftt.commit();
} else if (id == R.id.nav_climat) {
FragmentTransaction ftt = getSupportFragmentManager().beginTransaction()
.replace(R.id.main_activity_container, new ClimatFragment()).addToBackStack(null);
ftt.commit();
} else if (id == R.id.nav_chat) {
FragmentTransaction ftt = getSupportFragmentManager().beginTransaction()
.replace(R.id.main_activity_container, new ChatFragment()).addToBackStack(null);
ftt.commit();
} else if (id == R.id.nav_logout) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(MainActivity.this, LoginActivity.class));
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
positionData = new PositionData();
positionData.setLatitude(location.getLatitude());
positionData.setLongitude(location.getLongitude());
positionData.setTimestamp(System.currentTimeMillis() / 1000);
MyUtils myUtils = new MyUtils();
positionData.setNetworkType(myUtils.getNetworkClass(this));
Log.d(TAG, "Changed");
mFirebaseDatabaseReference.child("positions").child(uid).child("" + System.currentTimeMillis() / 1000).setValue(positionData);
}
}
我的日志错误
E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.esprit.couverture4g/com.esprit.couverture4g.activities.MainActivity}: java.lang.IllegalArgumentException: invalid provider: null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalArgumentException: invalid provider: null
at android.location.LocationManager.checkProvider(LocationManager.java:1704)
at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1194)
at com.esprit.couverture4g.activities.MainActivity.onCreate(MainActivity.java:107)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.esprit.couverture4g, PID: 29830
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.esprit.couverture4g/com.esprit.couverture4g.activities.MainActivity}: java.lang.IllegalArgumentException: invalid provider: null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalArgumentException: invalid provider: null
at android.location.LocationManager.checkProvider(LocationManager.java:1704)
at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1194)
at com.esprit.couverture4g.activities.MainActivity.onCreate(MainActivity.java:107)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)