当我点击一个按钮时,我想将短信发送到固定号码(实际上在两个虚拟设备之间),但是当我按下它时会抛出此异常。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.expert.myapplication, PID: 4454
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:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
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:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.SecurityException: Sending SMS message: uid 10079 does not have android.permission.SEND_SMS.
at android.os.Parcel.readException(Parcel.java:2004)
at android.os.Parcel.readException(Parcel.java:1950)
at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:789)
at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:329)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:312)
at com.example.expert.myapplication.MainActivity.onClick(MainActivity.java:103)
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:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
这里有一些简单的代码,我发现它应该适用于两台运行在vms中的手机,但这里并没有明显的原因
public class MainActivity extends AppCompatActivity{
private Button button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button3= (Button) findViewById(R.id.SEND);
}
public void onClick(View view) {
String number = "6942649512";
String message= "Help";
//Intent intent = new Intent(getApplicationContext(),MainActivity.class);
// PendingIntent pi=PendingIntent.getActivity(getApplicationContext(),0,intent,0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(number, null,message,null,null);
Toast toast = Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_SHORT);
toast.show();
}
}
我还在manifest.xml文件中添加了sms权限。
答案 0 :(得分:0)
你的例外是:
Caused by: java.lang.SecurityException: Sending SMS message: uid 10079 does not have android.permission.SEND_SMS.
这意味着您无权发送短信。因此,请尝试将Android权限添加到Android Manifest中,如果您使用API 23> =,则需要请求用户权限。
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS},1);
AndroidManifest:
<uses-permission android:name="android.permission.SEND_SMS"/>
请详细了解runtime permissions。