这是我的代码,如果在第105行中添加if语句,则程序崩溃,如果我删除了该代码,则我的代码正在工作。但是没有这个我就无法解决我的代码。请任何人告诉我这可能是问题还是我做错了什么?

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;
}
}

最佳答案

您的意思是您需要在这行中添加if语句吗?:

     catch (Exception e) {
     Toast.makeText(getApplicationContext(),
     "SMS faild, please try again.",
     Toast.LENGTH_LONG).show();
     e.printStackTrace();
}


请在未编译的语句中添加该语句。

07-24 09:17