I have developed an app that sends the user's location via SMS. When I starting building it there were no errors, but when it is being tested with an actual phone it crashes ... It would not open.
public class MainActivity extends AppCompatActivity {
EditText phone;
Button emergency;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phone= (EditText) findViewById(R.id.number);
emergency= (Button) findViewById(R.id.emergency);
emergency.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GPStracker g = new GPStracker(getApplicationContext());
Location l = g.getLocation();
if (l != null) {
double lat = l.getLatitude();
double lon = l.getLongitude();
String message = "http://maps.google.com/maps?saddr=" + lat + "," + lon;
String number = "number";
SmsManager smsManager = SmsManager.getDefault();
StringBuffer smsBody = new StringBuffer();
smsBody.append(Uri.parse(message));
android.telephony.SmsManager.getDefault().sendTextMessage(number, null, smsBody.toString(), null, null);
}
}
});
}
AND ALSO HERE IS MY CLASS FOR GPS TRACKING
public class GPStracker implements LocationListener {
Context context;
public GPStracker(Context c){
context = c;
}
public Location getLocation(){
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled){
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,10,this);
Location l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
return l;
}else{
Toast.makeText(context,"Please enable GPS", Toast.LENGTH_LONG).show();
}
return null;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
}
@Override
public void onProviderDisabled(String provider) {
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
}
}
AND MY MANIFEST FILE
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.research.sos.smshelpcaller">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I ALREADY TRY EVERYTHING BUT I DON'T KNOW WHAT IS WRONG IN MY CODES OR IS THERE SOMETHING MISSING.
LASTLY HERE IS THE LOGS IN LOGCAT
04-03 13:55:56.472 31224-31224/com.research.sos.smshelpcaller E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.research.sos.smshelpcaller, PID: 31224
java.lang.NullPointerException: Attempt to get length of null array
at android.os.Parcel.readException(Parcel.java:1546)
at android.os.Parcel.readException(Parcel.java:1493)
at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:1430)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:320)
at com.research.sos.smshelpcaller.MainActivity$1.onClick(MainActivity.java:37)
at android.view.View.performClick(View.java:5052)
at android.view.View$PerformClick.run(View.java:20162)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5753)
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:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)