E/AndroidRuntime:致命异常:主进程错误 Android Studio
E/AndroidRuntime: FATAL EXCEPTION: main Process error Android Studio
我写了一个发送短信的程序,执行时崩溃
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnsend = (Button) findViewById(R.id.btnsend);
final EditText edtnum = (EditText) findViewById(R.id.edtnum);
final EditText edttext = (EditText) findViewById(R.id.edttext);
btnsend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String phoneNo=edtnum.getText().toString();
String message=edttext.getText().toString();
SmsManager manager=SmsManager.getDefault();
manager.sendTextMessage(phoneNo,null,message,null,null);
}
});
}
}
应用程序在点击按钮时崩溃并给出此错误
2019-09-09 10:30:33.384 2104-2104/com.example.video60
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.video60, PID: 2104
java.lang.SecurityException: Sending SMS message: uid 10086 does not have android.permission.SEND_SMS.
at android.os.Parcel.readException(Parcel.java:1942)
at android.os.Parcel.readException(Parcel.java:1888)
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.video60.MainActivity.onClick(MainActivity.java:41)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
在您的 android 清单中,声明以下内容:
<uses-permission android:name="android.permission.SEND_SMS" />
正如您的异常告诉您的那样:
Sending SMS message: uid 10086 does not have
android.permission.SEND_SMS.
您的应用没有发送短信的权限
查看此以获取更多信息:https://developer.android.com/guide/topics/permissions/overview
我的回答适用于 api 的 22 岁及以下,对于任何更高的你都必须请求许可,你可以在这里找到:https://developer.android.com/training/permissions/requesting
像这样:
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
}
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.SEND_SMS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
// Permission has already been granted
}
我写了一个发送短信的程序,执行时崩溃
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnsend = (Button) findViewById(R.id.btnsend);
final EditText edtnum = (EditText) findViewById(R.id.edtnum);
final EditText edttext = (EditText) findViewById(R.id.edttext);
btnsend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String phoneNo=edtnum.getText().toString();
String message=edttext.getText().toString();
SmsManager manager=SmsManager.getDefault();
manager.sendTextMessage(phoneNo,null,message,null,null);
}
});
}
}
应用程序在点击按钮时崩溃并给出此错误
2019-09-09 10:30:33.384 2104-2104/com.example.video60 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.video60, PID: 2104 java.lang.SecurityException: Sending SMS message: uid 10086 does not have android.permission.SEND_SMS. at android.os.Parcel.readException(Parcel.java:1942) at android.os.Parcel.readException(Parcel.java:1888) 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.video60.MainActivity.onClick(MainActivity.java:41) at android.view.View.performClick(View.java:6256) at android.view.View$PerformClick.run(View.java:24697) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
在您的 android 清单中,声明以下内容:
<uses-permission android:name="android.permission.SEND_SMS" />
正如您的异常告诉您的那样:
Sending SMS message: uid 10086 does not have android.permission.SEND_SMS.
您的应用没有发送短信的权限
查看此以获取更多信息:https://developer.android.com/guide/topics/permissions/overview
我的回答适用于 api 的 22 岁及以下,对于任何更高的你都必须请求许可,你可以在这里找到:https://developer.android.com/training/permissions/requesting
像这样:
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
}
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.SEND_SMS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
// Permission has already been granted
}