2014-03-13 45 views
0

继承人我的代码通过短信在一个活动中发送GPS坐标

如果我发送纯字符串作为文本,它工作正常。然而,当我尝试发送坐标时,它崩溃。 我已经尝试使用两个活动,以及;在那里,我永远不会发送短信。

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

您能否突出显示这一行:'ShowLocationActivity.java:59'? – donfuxx

+0

“locationManager.getLastKnownLocation(provider);'可能返回null的任何情况?这可能是...... – dgimenes

+0

尝试使用类似于位置的当前位置上的空检查。 – Cenarius

回答

0

由于您使用的仿真器,该问题可能是从这些行:

Location currentlocation = locationManager.getLastKnownLocation(provider); 
smsBody.append(currentlocation.getLatitude()); 
smsBody.append(currentlocation.getLongitude()); 

currentlocation将几乎永远是零,因为没有最后已知的位置。如果你做一个空的检查像以前的位置,它不应该崩溃。我可能是错的。

哦,别忘了打开你的模拟器的GPS。

+0

就是这样,仍然是应用程序崩溃。 – Cenarius

+0

即使在将坐标发送到模拟器后启动应用程序,崩溃。 – Cenarius

+0

如果你用smsBody注释掉所有的东西,它还会崩溃吗? – Andy