从编辑文本中获取文本并将字符串放入smsManager.sendTextMessage

时间:2015-06-17 13:43:52

标签: android android-edittext sms

从编辑文本中获取文本并将字符串放入smsManager.sendTextMessage。应用程序崩溃。如果我做String sms =" test"并使用它为textmessage它工作正常,但当我尝试使用编辑时的应用程序崩溃。

public class MainActivity extends ActionBarActivity {

private boolean detectEnabled;
public TextView smsText;
public String smsMessage;
private ImageButton buttonToggleDetect;

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

    smsText = (TextView) findViewById(R.id.smsText);
    smsMessage = smsText.getText().toString();
    buttonToggleDetect = (ImageButton) findViewById(R.id.buttonToggleDetect);

    buttonToggleDetect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            setDetectEnabled(!detectEnabled);
        }
    });
}

呼叫助手类

public class CallHelper extends MainActivity {

public String Test = "TestString";
public Boolean calling = false;
public MainActivity mainActivity;


public CallHelper() {

}

// Listener to detect incoming calls.
public class  CallStateListener extends PhoneStateListener{
    @Override

    public  void onCallStateChanged (int state, String incomingNumber) {
        switch (state){

            case TelephonyManager.CALL_STATE_RINGING:
                // case when someone is calling the users phone
                calling = true;

                Toast.makeText(ctx1,
                                "test3: " + incomingNumber, Toast.LENGTH_LONG).show();



                String phoneNumber = incomingNumber.toString();
                //EditText textBox = (EditText) findViewById(R.id.smsText);

                String  sms = mainActivity.smsMessage;
                try {
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(phoneNumber,null,sms,null,null);

                    Toast.makeText(ctx1,"Sms sent SuccessFully", Toast.LENGTH_LONG).show();

                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(ctx1,"Sms Failed", Toast.LENGTH_LONG).show();
                }
                break;


        }
    }
}

LogCat 06-17 14:30:11.529 919-919/? E/ActivityThread﹕ Failed to find provider info for com.tmobile.driving.settingsprovider 06-17 14:30:11.649 3648-3648/westport.andrewirwin.com.drivesafe E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at westport.andrewirwin.com.drivesafe.CallHelper$CallStateListener.onCallStateChanged(CallHelper.java:46) at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:593) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:155) at android.app.ActivityThread.main(ActivityThread.java:5454) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796) at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

当你从editText中读取时,请使用:

EditText textBox = (EditText) findViewById(R.id.smsText);
String sms = textBox.getText().getString();

此外,请确保您在Android.manifest中拥有权限SEND_SMS和WRITE_SMS

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />