选择微调器项目时出现空指针异常

时间:2014-04-30 06:29:46

标签: android spinner alertdialog

在我的应用程序中,我正在获取收到的消息详细信息并使用各种格式来放置这些详细信息并将新消息发送到另一个号码。要选择正文格式,我在选项菜单中提供了3种格式的下拉微调器。要获取的详细信息是发件人的号码和消息日期。但是我遇到的问题是当我打开对话框来选择一种体型。当我在Spinner中单击正文格式时,应用程序崩溃,显示java.NullPointerException。请检查我的代码并提供有用的编辑,如果有的话,谢谢。

case R.id.action_settings:

            final AlertDialog.Builder rdialog = new AlertDialog.Builder(MainActivity.this);
            rdialog.setTitle("Select Message Format")
            LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
            alertView = inflater.inflate(R.layout.rptsetting,null);

            final Spinner fSpinner = (Spinner)alertView.findViewById(R.id.fSpinner);
            final String format[] ={"- Select -","Format 1", "Format 2", "Format 3"};
            ArrayAdapter<String> adp = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_dropdown_item,providers);
            fSpinner.setAdapter(adp);
            fSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
                        int position, long id) {


                    if(fSpinner.getSelectedItem().toString().equals(format[0])){
                        body = null;
                    }
                    else if(fSpinner.getSelectedItem().toString().equals(format[1])){
                        body = "Sender's No "+sendaddress+"; Date and Time "+vtime.get(i)+"; Some Text"+reltxt.getText().toString();
                    }

                    else if(fSpinner.getSelectedItem().toString().equals(format[2])){
                        body = "Sender's No"+sendaddress+", Date and Time "+time.get(i)+", Some Text "+reltxt.getText().toString();
                    }

                    else if(fSpinner.getSelectedItem().toString().equals(format[3])){
                        body = "Sender's No "+sendaddress+", Date and Time "+time.get(i)+", Some Text "+reltxt.getText().toString();
                    }   

                }
                @Override
                public void onNothingSelected(AdapterView<?> aparent) {
                }


            });

            final Spinner phSpinner = (Spinner)alertView.findViewById(R.id.phSpinner);
            final String pnumber[] ={"- Select -","123456", "7889003"};
            ArrayAdapter<String> cno = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_dropdown_item,compno);
            phSpinner.setAdapter(cno);

            phSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> phParent, View item,
                        int post, long arg3) {


                    if(phSpinner.getSelectedItem().toString().equals(pnumber[0])){
                        phoneNo = null;
                    }
                    else if(phSpinner.getSelectedItem().toString().equals(pnumber[1])){
                        phoneNo="123456";
                    }
                    else if(phSpinner.getSelectedItem().toString().equals(pnumber[2])){
                        phoneNo=7889003;
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> phParent) {
                }
            });

            rdialog.setView(alertView);
            rdialog.setNeutralButton("SUBMIT", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();   
                    }
                });

            AlertDialog rdialog1 = rdialog.create();
            rdialog1.show();

private void sendSms()
 {   
         if(list.size()>0){
             for(int i=0;i<list.size();i++)
             {
                 if(list.get(i).isSelected())
                 {
                     if(sendaddress.equals(""))
                         sendaddress =list.get(i).getAddress();
                     else
                         sendaddress =list.get(i).getAddress();
                     try 
                     {
                        String SENT      = "SMS_SENT";
                         PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
                         registerReceiver(new BroadcastReceiver() 
                         {
                             @Override
                             public void onReceive(Context arg0, Intent arg1) 
                             {
                                 int resultCode = getResultCode();
                                 switch (resultCode) 
                                 {
                                    case Activity.RESULT_OK:                      
                                        Toast.makeText(getBaseContext(), "SMS Sent!!!!!", 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(), "No service",Toast.LENGTH_SHORT).show();
                                    break;

                                    case SmsManager.RESULT_ERROR_NULL_PDU:         
                                        Toast.makeText(getBaseContext(), "Null PDU",Toast.LENGTH_SHORT).show();
                                    break;

                                    case SmsManager.RESULT_ERROR_RADIO_OFF:        
                                        Toast.makeText(getBaseContext(), "Radio off",Toast.LENGTH_SHORT).show();
                                    break;
                                 }
                             }
                         }, new IntentFilter(SENT));

                         SmsManager smsMgr = SmsManager.getDefault();
                         smsMgr.sendTextMessage(numformat, null, body, sentPI, null);
                         delete();
                     }
                     catch (Exception e) 
                     {
                         Toast.makeText(this, e.getMessage()+"!\n"+"SMS failed, please try again", Toast.LENGTH_SHORT).show();
                         e.printStackTrace();
                     }
                 }
             }
         }
     else
     {
         Toast.makeText(this, SimState+ " " + "Cannot send SMS", Toast.LENGTH_SHORT).show();
     } 
 }

1 个答案:

答案 0 :(得分:1)

尝试在onItemSelected

中使用以下内容
String selectedItem = aparent.getItemAtPosition(pos).toString();

而不是

String selectedItem = aparent.getSelectedItem().toString();
  1. getItemAtPosition(pos)

  2. getSelectedItem()

  3. 希望它对你有所帮助。