应用程序在sendText方法崩溃

时间:2016-06-27 23:04:47

标签: java android android-intent sms

我正在开发我的第一个Android应用程序,有人可以将多个短信发送到指定的号码。当我将已经编入的消息的数量和数量编入代码时,应用程序运行正常。但是,我创建了一个单独的活动,可以输入目标号码和要发送的消息数量,并通过意图将该数据传回第一个活动。但是现在应用程序在尝试发送消息时崩溃了。

这是第一项活动:

public class TextMessages extends AppCompatActivity {
public static final String DEFAULT = "No Number Found";
private EditText customMessage;
String gotNum;
FloatingActionButton button;
int request_code;
String realnum;
int temp;
int recode;
Button butt;
int number;
String num;
String text;
int x;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_text_messages);
    request_code = 1;

    FloatingActionButton button = (FloatingActionButton) findViewById(R.id.but);

    assert button != null;
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(TextMessages.this,Settings.class);
            startActivityForResult(intent, request_code);

            Intent a = new Intent(TextMessages.this,Settings.class);
            startActivityForResult(a, recode);

        }
    });






    customMessage = (EditText)findViewById(R.id.customMessage);

}


public void onActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode== request_code){
        if (resultCode== Activity.RESULT_OK){
            num = data.getStringExtra("value");
            text = data.getStringExtra("cost");
        }

    }

}


public void sendText(View view)
{
    String message = "";
    if (view.getId() == R.id.fab)
    {
        message = customMessage.getText().toString();
    }
    else
    {
        Button sender = (Button)view;
        message = sender.getText().toString();
    }
    //Toast.makeText(this, "Test", Toast.LENGTH_SHORT).show();

    int i;
    x = Integer.parseInt(text);
    for (i = 0; i < x; i++){
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(num, null, message, null, null);


    }

}}

正如您所看到的,我已使用String num替换预定数字以及使用int x发送的消息数,这会导致应用程序在按下发送消息的按钮时崩溃。

以下是从以下位置收集要发送的号码和消息的活动:

public class Settings extends AppCompatActivity {
public EditText phoneNum;
public Button apply;
public EditText msgCounter;
public Button save;
int msgCount;


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

    phoneNum = (EditText)findViewById(R.id.phoneNum);
    msgCounter = (EditText)findViewById(R.id.msgCount);
    apply = (Button)findViewById(R.id.butto);
    save = (Button)findViewById(R.id.butt);



    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent pass = new Intent();
            pass.putExtra("value", phoneNum.getText().toString());
            pass.putExtra("cost",msgCounter.getText().toString());
            setResult(Activity.RESULT_OK,pass);
            finish();
        }
    });


}
public void OnButtonClickedApply(View view)

{
    String num;
    num = phoneNum.getText().toString();


    //Toast.makeText(this, num, Toast.LENGTH_SHORT).show();

}
public void saveMsg(View view)
  {
    msgCount = Integer.parseInt(msgCounter.getText().toString());
  }

}

我还可以在将数据输入第二个活动时添加。按下按钮保存数据后,活动重置(重新加载?),我必须再次输入数据,按下按钮后,我会回到第一个活动。

崩溃日志:

06-27 19:14:55.583 2404-2404/com.linfirmware.emergencytext E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.linfirmware.emergencytext, PID: 2404
                                                                         java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                             at android.view.View.performClick(View.java:5198)
                                                                             at android.view.View$PerformClick.run(View.java:21147)
                                                                             at android.os.Handler.handleCallback(Handler.java:739)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                          Caused by: java.lang.reflect.InvocationTargetException
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                             at android.view.View.performClick(View.java:5198) 
                                                                             at android.view.View$PerformClick.run(View.java:21147) 
                                                                             at android.os.Handler.handleCallback(Handler.java:739) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                             at android.os.Looper.loop(Looper.java:148) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                          Caused by: java.lang.SecurityException: Sending SMS message: uid 10057 does not have android.permission.SEND_SMS.
                                                                             at android.os.Parcel.readException(Parcel.java:1599)
                                                                             at android.os.Parcel.readException(Parcel.java:1552)
                                                                             at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:768)
                                                                             at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:310)
                                                                             at android.telephony.SmsManager.sendTextMessage(SmsManager.java:293)
                                                                             at com.example.rahul.customtext.TextMessages.sendText(TextMessages.java:94)
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                             at android.view.View.performClick(View.java:5198) 
                                                                             at android.view.View$PerformClick.run(View.java:21147) 
                                                                             at android.os.Handler.handleCallback(Handler.java:739) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                             at android.os.Looper.loop(Looper.java:148) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
06-27 19:14:57.765 2404-2404/com.linfirmware.emergencytext I/Process: Sending signal. PID: 2404 SIG: 9

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rahul.customtext">

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".TextMessages">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Settings"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

1 个答案:

答案 0 :(得分:0)

如果您使用的是Android M,则可能会关闭SEND_SMS权限。检查在模拟器或设备上的应用程序提供的权限,无论您在何处运行它。