第一个问题
我是Android Studio的初学者。我想创建短信应用。我将两个EditText用于编号,另一个用于xml中的文本,还有一个发送按钮,如下所示。
所以当我点击发送按钮然后显示它unfortunately app has stopped
。程序中没有错误。
第二个问题
这种问题究竟是什么原因?
非常感谢您在这件事上的时间和帮助。
public class MainActivity extends AppCompatActivity {
EditText etMassage;
EditText etTelNr;
int MY_PERMISSION_REQUEST_SEND_SMS = 1;
String SENT = "SMS_SEND";
String DELIVERD = "SMS_DELIVERD";
PendingIntent sentp1, deliverdP1;
BroadcastReceiver sendsmsReciver, smsDeliveredReciver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etMassage = (EditText) findViewById(R.id.etMassage);
etTelNr = (EditText) findViewById(R.id.etTelNr);
sentp1 = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
deliverdP1 = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERD), 0);
}
@Override
protected void onResume() {
super.onResume();
sendsmsReciver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(MainActivity.this, "SMS sent ", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(MainActivity.this, "generic failure ", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(MainActivity.this, "NO SERVICE ", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(MainActivity.this, "NO PDU ", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(MainActivity.this, " NO RADIO", Toast.LENGTH_SHORT).show();
break;
}
}
};
smsDeliveredReciver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(MainActivity.this, "SMS DELIVERED ", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(MainActivity.this, "SMS CANCELLED ", Toast.LENGTH_SHORT).show();
break;
}
}
};
registerReceiver(sendsmsReciver, new IntentFilter(SENT));
registerReceiver(smsDeliveredReciver, new IntentFilter(DELIVERD));
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(smsDeliveredReciver);
unregisterReceiver(sendsmsReciver);
}
public void btn_SendSMS_OnClick(View v) {
String message = etMassage.getText().toString();
String telNr = etTelNr.getText().toString();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, MY_PERMISSION_REQUEST_SEND_SMS);
} else {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(telNr, null, message, sentp1, deliverdP1);
}
}
}
按钮的
<Button
android:text="Send"
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="send" />
答案 0 :(得分:0)
您在按钮的OnClick事件中调用了错误的函数 它应该是这样的
<Button
android:text="Send"
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="btn_SendSMS_OnClick" />
希望这能解决问题。