java.lang.NullPointerException:尝试在发送多个短信时获取空数组的长度

时间:2018-07-22 10:35:23

标签: android arraylist nullpointerexception sms

我已经搜索了几种解决方案,但是没有用。我想从“原始”文件夹中读取CSV文件,并以CSV文件中的编号发送多个SMS。当我运行时,出现以下错误“尝试获取null数组的长度”会导致应用崩溃。我尝试了几种方法,但没有任何效果。我在清单中也具有适当的权限。

ArrayList<String> MobNumber = new ArrayList<>(100);

    private final int MY_PERMISSIONS_REQUEST_SEND_SMS = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);
            final Button btnBroadCast = findViewById(R.id.buttonBroadcast);
            btnBroadCast.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    InputStream inputStream = getResources().openRawResource(R.raw.csv_copy);
                    CSVFile csvFile = new CSVFile(inputStream);
                    List<String[]> scoreList = csvFile.read();

                    for(String[] scoreData:scoreList ) {
                        MobNumber.add(Arrays.toString(scoreData));
                    }
                    callSendSmsAPI();
                    ImageView hScreen = findViewById(R.id.homeScreen);
                    //hScreen.setBackgroundResource(R.drawable.ready3);

                    btnBroadCast.setVisibility(View.GONE);
                    hScreen.setVisibility(View.GONE);
                }
            });
    }

    private void callSendSmsAPI() {
        final PulsatorLayout pulsator = findViewById(R.id.pulsator);
        final ImageView pCenter = findViewById(R.id.pulsatorCenter);
        pulsator.start();
        if (pulsator.isStarted()) {
            pCenter.setVisibility(View.VISIBLE);
        }
        for (int i = 0; i < MobNumber.size(); i++) {
            String message = "hello world";
            String tempMobileNumber = MobNumber.get(i);
            //msisdn = MobNumber.get(i);
            MultipleSMS(tempMobileNumber, message);
            //callUpdateApi(tempMobileNumber);
        }
    }

    public void stopPulse() {
        PulsatorLayout pulsator = findViewById(R.id.pulsator);
        if (pulsator.isStarted()) {

            pulsator.stop();
        }
    }

    public void showSuccessImage() {
        ImageView ivSuccess = findViewById(R.id.imageView_success);
        ivSuccess.setVisibility(View.VISIBLE);
        // callUpdateContentApi();
    }

    public void showFailedImage() {
        ImageView ivFailed = findViewById(R.id.imageView_fail);
        ivFailed.setVisibility(View.VISIBLE);
    }

    private void MultipleSMS(final String phoneNumber, final String message) {
        final String SENT = "SMS_SENT";
        final String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
                SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                new Intent(DELIVERED), 0);

        // ---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        ContentValues values = new ContentValues();
                        for (int i = 0; i < MobNumber.size(); i++) {
                            values.put("address", MobNumber.get(i));
                            values.put("body", message);

                        }
                        getContentResolver().insert(
                                Uri.parse("content://sms/sent"), values);


                        //stopPulse();

                        //showSuccessImage();


                        Toast.makeText(getBaseContext(), "SMS sent",
                                Toast.LENGTH_SHORT).show();


                        //callUpdateApi(msisdn);

                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:


                        Log.d("generic", "generic fail");
                        stopPulse();
                        Toast.makeText(getBaseContext(), "Generic failure",
                                Toast.LENGTH_SHORT).show();

                        showFailedImage();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:

                        Log.d("no service", "service fail");
                        stopPulse();
                        Toast.makeText(getBaseContext(), "No service",
                                Toast.LENGTH_SHORT).show();

                        showFailedImage();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:


                        Log.d("null PDU", "api Fail");
                        stopPulse();
                        Toast.makeText(getBaseContext(), "Null PDU",
                                Toast.LENGTH_SHORT).show();

                        showFailedImage();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:

                        showFailedImage();
                        Log.d("radio off", "api Fail");
                        stopPulse();
                        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:

                        stopPulse();
                        showSuccessImage();

                        Toast.makeText(getBaseContext(), "SMS delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:

                        showFailedImage();
                        Toast.makeText(getBaseContext(), "SMS not delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(DELIVERED));

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.SEND_SMS)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.SEND_SMS)) {
            } else {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.SEND_SMS},
                        MY_PERMISSIONS_REQUEST_SEND_SMS);
            }
        }
    }

CSV格式:01937970970,01556352512

调试器值 Debugger values

错误日志 enter image description here

1 个答案:

答案 0 :(得分:0)

您的数组为null,这表示无法正确解析csv数据。它没有在MobNumber数组中插入任何电话号码。 在callSendSmsAPI()的开头检查此数组是否为null,如果不为null,则仅继续。