我正在尝试构建一个使用短信发送gps位置的应用,我遇到了这个错误:
SmsManager类型中的方法sendTextMessage(String,String,String,PendingIntent,PendingIntent)不适用于参数(EditText,null,String,null,null)
package com.example.thermolocation;import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity
{
EditText txtphoneNo;
String Text;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
Text = "My location is: " +
"Latitude = " + loc.getLatitude() +
"Longitude = " + loc.getLongitude();
Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
txtphoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
sendSMSMessage();
}
protected void sendSMSMessage() {
Log.i("Send SMS", "");
try {
android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();
smsManager.sendTextMessage(txtphoneNo, null, Text, null, null); /* <--here i got the error */
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"GPS Disabled",
Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),
"GPS Enabled",
Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
我可以帮忙吗? 谢谢
答案 0 :(得分:1)
检查Error
:
The method sendTextMessage(String, String, String, PendingIntent, PendingIntent) in the type SmsManager is not applicable for the arguments (EditText, null, String, null, null)
第一个参数不匹配,您传递的是EditText而不是String。 我建议您更改如下:
smsManager.sendTextMessage(txtphoneNo.getText().toString(), null, Text, null, null);
答案 1 :(得分:0)
错误很明显:
方法是:
sendTextMessage(String, String, String, PendingIntent, PendingIntent)
但你用参数(EditText, null, String, null, null)
注意第一个参数是EditText
而不是String
(它想要的)
您应该使用txtphoneNo.getText().toString()
答案 2 :(得分:0)
EditText不是字符串。将您的电话改为:
smsManager.sendTextMessage(txtphoneNo.getText().toString(), null, Text, null, null);