【发布时间】:2017-08-17 08:24:18
【问题描述】:
第一个问题
我是 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);
}
}
}
按钮的xml代码
<Button
android:text="Send"
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="send" />
【问题讨论】:
-
在按钮 xml 中,有 android:onclick="send"。在 Activity 中,我们应该具有与 send 相同的方法名称。无效发送(查看视图){}
-
在 xml 文件中使用 android:text="btn_SendSMS_OnClick" 而不是 android:text="Send"
标签: android