继承我的代码
如果我发送纯字符串作为文本,它可以正常工作。 但是当我尝试发送坐标时,它会崩溃。 我也尝试过使用两项活动;我永远不会发短信。
public class ShowLocationActivity extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_location);
latituteField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
Button c = (Button) findViewById(R.id.button1);
final EditText recipientTextEdit = (EditText) ShowLocationActivity.this
.findViewById(R.id.EditView1);
Location currentlocation = locationManager.getLastKnownLocation(provider);
final StringBuffer smsBody = new StringBuffer();
smsBody.append("http://maps.google.com?q=");
smsBody.append(currentlocation.getLatitude());
smsBody.append(",");
smsBody.append(currentlocation.getLongitude());
c.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String phoneNo = recipientTextEdit.getText().toString();;
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, smsBody.toString(), null, null);
Toast.makeText(getApplicationContext(), "Request Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
" Error, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}});
}
/* Request updates at startup */
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
应用程序在模拟器中崩溃。
我的logcat
03-13 18:09:11.300: E/AndroidRuntime(802): FATAL EXCEPTION: main
03-13 18:09:11.300: E/AndroidRuntime(802): Process: com.example.android.locationapi.simple, PID: 802
03-13 18:09:11.300: E/AndroidRuntime(802): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.locationapi.simple/com.example.android.locationapi.simple.ShowLocationActivity}: java.lang.NullPointerException
03-13 18:09:11.300: E/AndroidRuntime(802): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-13 18:09:11.300: E/AndroidRuntime(802): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-13 18:09:11.300: E/AndroidRuntime(802): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-13 18:09:11.300: E/AndroidRuntime(802): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-13 18:09:11.300: E/AndroidRuntime(802): at android.os.Handler.dispatchMessage(Handler.java:102)
03-13 18:09:11.300: E/AndroidRuntime(802): at android.os.Looper.loop(Looper.java:137)
03-13 18:09:11.300: E/AndroidRuntime(802): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-13 18:09:11.300: E/AndroidRuntime(802): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 18:09:11.300: E/AndroidRuntime(802): at java.lang.reflect.Method.invoke(Method.java:515)
03-13 18:09:11.300: E/AndroidRuntime(802): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-13 18:09:11.300: E/AndroidRuntime(802): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-13 18:09:11.300: E/AndroidRuntime(802): at dalvik.system.NativeStart.main(Native Method)
03-13 18:09:11.300: E/AndroidRuntime(802): Caused by: java.lang.NullPointerException
03-13 18:09:11.300: E/AndroidRuntime(802): at com.example.de.vogella.android.locationapi.simple.ShowLocationActivity.onCreate(ShowLocationActivity.java:59)
03-13 18:09:11.300: E/AndroidRuntime(802): at android.app.Activity.performCreate(Activity.java:5243)
03-13 18:09:11.300: E/AndroidRuntime(802): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-13 18:09:11.300: E/AndroidRuntime(802): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-13 18:09:11.300: E/AndroidRuntime(802): ... 11 more
答案 0 :(得分:0)
由于您正在使用模拟器,问题可能来自以下几行:
Location currentlocation = locationManager.getLastKnownLocation(provider);
smsBody.append(currentlocation.getLatitude());
smsBody.append(currentlocation.getLongitude());
currentlocation几乎总是为null,因为没有最后的已知位置。如果您像以前的位置一样执行空检查,则不应该崩溃。我错了。
哦,别忘了为模拟器打开GPS。