无法弄清楚这个SMSmanager错误

时间:2013-05-11 20:54:44

标签: android sms

第一次尝试短信。从wrox书中复制代码并且它有效,但是现在当我尝试插入输入自定义数字和自定义文本的能力时,我得到了一个我无法弄清楚的错误(目的地地址无效......但我是从1个模拟器转到另一个使用该示例的...当我输入数字时失败)。这是主要活动:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.telephony.SmsManager;

public class SMSActivity extends Activity {

    public String contactNumber;
    public String messageText;

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

        EditText contact_Number = (EditText)findViewById(R.id.contact_number);
        contactNumber = contact_Number.getText().toString();

        EditText text_Message = (EditText)findViewById(R.id.text_message);
        messageText = text_Message.getText().toString();
    }

    public void onClick(View v) {    

        sendSMS(contactNumber, messageText);
    }

    private void sendSMS(String phoneNumber, String message) {

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }
}

这是XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SMSActivity" >

    <EditText
        android:id="@+id/contact_number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true" >

        <EditText
            android:id="@+id/text_message"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@+id/btnSendSMS" />

        <Button
            android:id="@+id/btnSendSMS"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="send"
            android:onClick="onClick" />

    </RelativeLayout> 

</RelativeLayout>

如果您需要,这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.learn2develop.sms"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="net.learn2develop.sms.SMSActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我点击发送之前的样子:

enter image description here

最后,logcat:

05-11 17:31:05.313: E/AndroidRuntime(589): FATAL EXCEPTION: main
05-11 17:31:05.313: E/AndroidRuntime(589): java.lang.IllegalStateException: Could not execute method of the activity
05-11 17:31:05.313: E/AndroidRuntime(589):  at android.view.View$1.onClick(View.java:3039)
05-11 17:31:05.313: E/AndroidRuntime(589):  at android.view.View.performClick(View.java:3480)
05-11 17:31:05.313: E/AndroidRuntime(589):  at android.view.View$PerformClick.run(View.java:13983)
05-11 17:31:05.313: E/AndroidRuntime(589):  at android.os.Handler.handleCallback(Handler.java:605)
05-11 17:31:05.313: E/AndroidRuntime(589):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-11 17:31:05.313: E/AndroidRuntime(589):  at android.os.Looper.loop(Looper.java:137)
05-11 17:31:05.313: E/AndroidRuntime(589):  at android.app.ActivityThread.main(ActivityThread.java:4340)
05-11 17:31:05.313: E/AndroidRuntime(589):  at java.lang.reflect.Method.invokeNative(Native Method)
05-11 17:31:05.313: E/AndroidRuntime(589):  at java.lang.reflect.Method.invoke(Method.java:511)
05-11 17:31:05.313: E/AndroidRuntime(589):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-11 17:31:05.313: E/AndroidRuntime(589):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-11 17:31:05.313: E/AndroidRuntime(589):  at dalvik.system.NativeStart.main(Native Method)
05-11 17:31:05.313: E/AndroidRuntime(589): Caused by: java.lang.reflect.InvocationTargetException
05-11 17:31:05.313: E/AndroidRuntime(589):  at java.lang.reflect.Method.invokeNative(Native Method)
05-11 17:31:05.313: E/AndroidRuntime(589):  at java.lang.reflect.Method.invoke(Method.java:511)
05-11 17:31:05.313: E/AndroidRuntime(589):  at android.view.View$1.onClick(View.java:3034)
05-11 17:31:05.313: E/AndroidRuntime(589):  ... 11 more
05-11 17:31:05.313: E/AndroidRuntime(589): Caused by: java.lang.IllegalArgumentException: Invalid destinationAddress
05-11 17:31:05.313: E/AndroidRuntime(589):  at android.telephony.SmsManager.sendTextMessage(SmsManager.java:77)
05-11 17:31:05.313: E/AndroidRuntime(589):  at net.learn2develop.sms.SMSActivity.sendSMS(SMSActivity.java:34)
05-11 17:31:05.313: E/AndroidRuntime(589):  at net.learn2develop.sms.SMSActivity.onClick(SMSActivity.java:28)
05-11 17:31:05.313: E/AndroidRuntime(589):  ... 14 more

感谢所有人提前

1 个答案:

答案 0 :(得分:1)

答案刚刚跳了出来。我将我的字符串命名为contactNumber和messageText,但是调用了phoneNumber和message。更改对&#39; contactNumber&#39;的所有引用和&#39; messageText&#39;修好了