Java – Android Studio: App crashes on some phones

Android Studio: App crashes on some phones… here is a solution to the problem.

Android Studio: App crashes on some phones

I’m new to android development and confused by this issue.
I’m making a simple application that has 3 TextViews: text, text2, and text3.

  • text Displays the latitude of the user
  • text2 displays the user’s longitude and longitude
  • text3 shows the distance between the user and the place named “Azadi Square”.

The app works fine on the Samsung Galaxy S4 and Samsung Galaxy Tab S, but not on the Huawei Y511 and Sony Xperias One of them (I don’t know the exact name. ), the app cannot be opened and says App has unfortunately stopped

Here is my Java code:

public class Map extends Activity implements LocationListener {

private TextView text,text2,text3;
private String provider;
private LocationManager locationManager;
private double latitude,longitude;
private Location azadiSquare;

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

text = (TextView)findViewById(R.id.text);
    text2 = (TextView)findViewById(R.id.text2);
    text3 = (TextView)findViewById(R.id.text3);

locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria,false);

azadiSquare = new Location(provider);
    azadiSquare.setLatitude(35.6996540);
    azadiSquare.setLongitude(51.3379906);

Location location = locationManager.getLastKnownLocation(provider);

text.setText(location.getLatitude()+"");
    text2.setText(location.getLongitude()+"");
    text3.setText(distanceBetweenMeter(azadiSquare,location)+"");
}

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

locationManager.requestLocationUpdates(provider,400,1,this);
}

@Override
protected void onPause() {
    super.onPause();

locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
    latitude = location.getLatitude();
    longitude = location.getLongitude();

text.setText(latitude+"");
    text2.setText(longitude+"");

text3.setText(distanceBetweenMeter(azadiSquare,location)+"");
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
     TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String s) {
    Toast.makeText(this,"Enabled new provider " + provider,Toast.LENGTH_LONG).show();
}

@Override
public void onProviderDisabled(String s) {
    Toast.makeText(this,"Disabled provider " + provider,Toast.LENGTH_LONG).show();
}

private double getDistanceFromLatLonInKm(double lat1,double lon1,double lat2,double lon2) {
    double R = 6371;  Radius of the earth in km
    double dLat = deg2rad(lat2-lat1);   deg2rad below
    double dLon = deg2rad(lon2-lon1);
    double a =
            Math.sin(dLat/2) * Math.sin(dLat/2) +
                    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
                            Math.sin(dLon/2) * Math.sin(dLon/2)
            ;
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double d = R * c;  Distance in km
    return d;
}

private double deg2rad(double deg) {
    return deg * (Math.PI/180);
}

private int distanceBetweenMeter(Location location1, Location location2){
    double distanceKM = getDistanceFromLatLonInKm(location1.getLatitude(),location1.getLongitude(),
            location2.getLatitude(),location2.getLongitude());
    int distanceMeter = (int)(distanceKM*1000);
    return distanceMeter;
}
}

Note: I just deleted the import!

list file:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=". Map" android:screenOrientation="landscape">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Note: I opened my phone’s location before opening the app, so this is not the problem. Another thing I would like to say is that all the calculations are okay and they work well.

Solution

On Android 6.0 (API level 23) and later, you need to request ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION permissions at runtime. Look, Requesting Permissions at Run Time.

Related Problems and Solutions