这是我的代码,如果在第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;
}
}
答案 0 :(得分:2)
你的意思是你需要在这行中添加一个if语句吗?:
catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
请在不编译的地方添加声明。
答案 1 :(得分:0)
mSelectedItemExam
可能是null
,因为您当时没有选择
mSelectedItemExam.equals("Select One")
了解如何使用Logcat以及下次发布stracktrace时:)