mardi 26 mai 2015

need help on condition in android

this is my code, If add the if satament in line No 105 then programme going crashed, if i removed this then my code is working. But without this i can't solve my code. Please help me.

public class SMSDetails extends Activity {

Spinner examSpinnerSMS,yearSpinnerSMS,boardSpinnerSMS;
private String[] examinationStrings;
private String[] yearStrings;
private String[] boardStrings;
int index;
private String mSelectedItemExam,mSelectedItemYear,mSelectedItemBoard;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_smsdetails);

    //initialize view       
    examSpinnerSMS = (Spinner)findViewById(R.id.spnrExamination);
    yearSpinnerSMS=(Spinner)findViewById(R.id.spnrYear);
    boardSpinnerSMS=(Spinner)findViewById(R.id.spnrBoard);

    //initialize data source
    examinationStrings = getResources().getStringArray(R.array.Examination);
    yearStrings = getResources().getStringArray(R.array.YearArray);
    boardStrings = getResources().getStringArray(R.array.Board);

    //initialize view
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        this, android.R.layout.simple_spinner_item, examinationStrings);        
    ArrayAdapter<String> adapter1= new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_item, yearStrings);
    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_item, boardStrings);


    //bind adapter and view
    examSpinnerSMS.setAdapter(adapter);
    yearSpinnerSMS.setAdapter(adapter1);
    boardSpinnerSMS.setAdapter(adapter2);

    //work with the spinners
    examSpinnerSMS.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                  @Override
                  public void onItemSelected(AdapterView<?> arg0, View view,int arg2, long arg3) {

                   //index = examSpinnerSMS.getSelectedItemPosition();
                    mSelectedItemExam=arg0.getSelectedItem().toString();
                    //Toast.makeText(getApplicationContext(),"You have selected "+mSelectedItemExam,Toast.LENGTH_LONG).show();
                      // TODO Auto-generated method stub
                  }  
                  @Override
                  public void onNothingSelected(AdapterView<?> arg0) {
                      // TODO Auto-generated method stub     
                  }  
              }
          );
    yearSpinnerSMS.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View view,int arg2, long arg3) {

          //index = examSpinnerSMS.getSelectedItemPosition();
          mSelectedItemYear=arg0.getSelectedItem().toString();
          //Toast.makeText(getApplicationContext(),"You have selected "+mSelectedItemYear,Toast.LENGTH_LONG).show();
            // TODO Auto-generated method stub
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }    
    }
);
    boardSpinnerSMS.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View view,int arg2, long arg3) {

          index = examSpinnerSMS.getSelectedItemPosition();
          mSelectedItemBoard=arg0.getSelectedItem().toString();
          //Toast.makeText(getApplicationContext(),"You have selected "+mSelectedItemBoard,Toast.LENGTH_LONG).show();
            // TODO Auto-generated method stub
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }    
    }
);

  //Log.i("problem", "problem");
    //problem is here
if (mSelectedItemExam.equals("Select One")||mSelectedItemYear.equals("Select One")|| mSelectedItemBoard.equals("Select One")) {
    Toast.makeText(getApplicationContext(), "Enter all the value", Toast.LENGTH_SHORT).show();
}


//Log.i("problem", "problem");

}


public void Submit(View view) {     
    String fm = mSelectedItemExam+" "+mSelectedItemBoard+" "+mSelectedItemYear;
    //Log.i("problem", "problem");
     try {
         sendSMS("5556", fm);
         Toast.makeText(getApplicationContext(), "SMS sent",
         Toast.LENGTH_LONG).show();
         } 
     catch (Exception e) {
         Toast.makeText(getApplicationContext(),
         "SMS faild, please try again.",
         Toast.LENGTH_LONG).show();
         e.printStackTrace();
    }
}
 private void sendSMS(String phoneNumber, String message)
    {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.smsdetails, menu);
    return true;
}

}

Aucun commentaire:

Enregistrer un commentaire