我想在另一个calss中运行发送短信但是当我启动虚拟设备时它“不幸地......已经停止了”。
这是SensSms
public class SendSmsClass extends Activity {
public void sendSMS(String phoneNumber,String message) {
SmsManager smsManager = SmsManager.getDefault();
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
int messageCount = parts.size();
Log.i("Message Count", "Message Count: " + messageCount);
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
for (int j = 0; j < messageCount; j++) {
sentIntents.add(sentPI);
deliveryIntents.add(deliveredPI);
}
// ---when the SMS has been sent---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "دستور ارسال شد ، منتظر دریافت پاسخ باشید",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "عدم وجود سیگنال",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "خطای نامشخص",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
// ---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "دستور به دستگاه رسید",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "دستور به دستگاه نرسید",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
smsManager.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
/* sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents, deliveryIntents); */
}
}
我从另一个活动调用此类来发送此代码中的短信:
public class AdminNumber extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_number);
SendSmsClass ssc = new SendSmsClass();
ssc.sendSMS("3432", "test");
这是我的logcat:
06-23 04:47:56.945: W/dalvikvm(1093): threadid=1: thread exiting with uncaught exception (group=0xb2aedba8)
06-23 04:47:56.965: E/AndroidRuntime(1093): FATAL EXCEPTION: main
06-23 04:47:56.965: E/AndroidRuntime(1093): Process: com.example.gpsportalsms, PID: 1093
06-23 04:47:56.965: E/AndroidRuntime(1093): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gpsportalsms/com.example.gpsportalsms.AdminNumber}: java.lang.NullPointerException
06-23 04:47:56.965: E/AndroidRuntime(1093): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-23 04:47:56.965: E/AndroidRuntime(1093): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-23 04:47:56.965: E/AndroidRuntime(1093): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-23 04:47:56.965: E/AndroidRuntime(1093): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-23 04:47:56.965: E/AndroidRuntime(1093): at android.os.Handler.dispatchMessage(Handler.java:102)
06-23 04:47:56.965: E/AndroidRuntime(1093): at android.os.Looper.loop(Looper.java:136)
06-23 04:47:56.965: E/AndroidRuntime(1093): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-23 04:47:56.965: E/AndroidRuntime(1093): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 04:47:56.965: E/AndroidRuntime(1093): at java.lang.reflect.Method.invoke(Method.java:515)
06-23 04:47:56.965: E/AndroidRuntime(1093): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-23 04:47:56.965: E/AndroidRuntime(1093): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-23 04:47:56.965: E/AndroidRuntime(1093): at dalvik.system.NativeStart.main(Native Method)
06-23 04:47:56.965: E/AndroidRuntime(1093): Caused by: java.lang.NullPointerException
06-23 04:47:56.965: E/AndroidRuntime(1093): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
06-23 04:47:56.965: E/AndroidRuntime(1093): at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:478)
06-23 04:47:56.965: E/AndroidRuntime(1093): at android.app.PendingIntent.getBroadcast(PendingIntent.java:467)
06-23 04:47:56.965: E/AndroidRuntime(1093): at com.example.gpsportalsms.SendSmsClass.sendSMS(SendSmsClass.java:33)
06-23 04:47:56.965: E/AndroidRuntime(1093): at com.example.gpsportalsms.AdminNumber.onCreate(AdminNumber.java:27)
06-23 04:47:56.965: E/AndroidRuntime(1093): at android.app.Activity.performCreate(Activity.java:5231)
06-23 04:47:56.965: E/AndroidRuntime(1093): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-23 04:47:56.965: E/AndroidRuntime(1093): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-23 04:47:56.965: E/AndroidRuntime(1093): ... 11 more
但是当我在一个类文件中使用我的代码时,它运行得很好 什么是我的错?
答案 0 :(得分:0)
您应该从SendSms类中删除extends Activity,它确实有效..立即尝试
而且你需要发送内容的上下文然后你可以从AdminNumber Activity传递它的构造函数,
请参阅修改后的代码
public class SendSmsClass {
Context c;
SendSmsClass(Context c){
this.c=c;
}
public void sendSMS(String phoneNumber,String message) {
SmsManager smsManager = SmsManager.getDefault();
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
int messageCount = parts.size();
Log.i("Message Count", "Message Count: " + messageCount);
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
PendingIntent sentPI = PendingIntent.getBroadcast(c, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(c, 0, new Intent(DELIVERED), 0);
for (int j = 0; j < messageCount; j++) {
sentIntents.add(sentPI);
deliveryIntents.add(deliveredPI);
}
// ---when the SMS has been sent---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(c, "دستور ارسال شد ، منتظر دریافت پاسخ باشید",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(c, "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(c, "عدم وجود سیگنال",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(c, "خطای نامشخص",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(c, "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
// ---when the SMS has been delivered---
c.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(c, "دستور به دستگاه رسید",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(c, "دستور به دستگاه نرسید",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
smsManager.sendTextMessage(phoneNumber,null,message,sentPI,deliveredPI); / * sms.sendMultipartTextMessage(phoneNumber,null,parts,sentIntents,deliveryIntents); * / }
并在AdminNumber中:
SendSmsClass ssc = new SendSmsClass(AdminNumber.this);
ssc.sendSMS("3432", "test");
如果您需要上下文,请在任何地方使用 c
祝你好运