Reputation: 245
In my application I'm fetching received message details and using various format to put those details and send the new message to the other number. To select the body format I'm providing a dropdown spinner with 3 formats in option menu. The details to be fetched are Sender's no, and message date. But the problem that I'm facing is when I open Dialog box to select a body format. When I click the body format in the Spinner, the application crashes, showing a java.NullPointerException
. Please check my code and provide useful edits if any, Thanks.
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();
}
}
Upvotes: 0
Views: 1190
Reputation: 16739
Try using following in onItemSelected
String selectedItem = aparent.getItemAtPosition(pos).toString();
Instead of
String selectedItem = aparent.getSelectedItem().toString();
Hope it helps you.
Upvotes: 1