我的代码会崩溃,如果我没有评论声明,如果(message.equals(“holiday”))postexecute告诉我为什么不进一步如果不是euqal这一行(message.equals(“holiday”))为什么不打印“学校今天关闭。原因:如果我发表评论,如果(message.equals(”假期“))这个代码应用工作正常,请检查我的if else stateement
String message;
public class getDataTask extends AsyncTask<Void, Void, Void> {
getDataTask() {
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
yourBoolean=false;
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
displayData();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
yourBoolean=true ;
if ((Category_ID.size() > 0) ) {
listCategory.setAdapter(cla);
cla.notifyDataSetChanged() ;
listCategory.invalidateViews();
menu_nametxt.setText(mVal2);
}
else if (message.equals("holiday"))
{
menu_nametxt.setText("No menu available .");
listCategory.setVisibility(View.GONE);
}
else
menu_nametxt.setText("School is off today. Reason: "+mVal3);
listCategory.setVisibility(View.GONE);
}
private void displayData() {
Cursor mCursor3 = db.selectQuery("SELECT * FROM uss_vacation WHERE calendar_id);
if (mCursor3.moveToFirst()) {
do {
Vacation_Date.add(mCursor3.getString(mCursor3.getColumnIndex("date")));
if(mCursor3.getString(mCursor3.getColumnIndex("date")).equals(mydate))
{
message = "holiday";
String mVal ;
mVal = (mCursor3.getString(mCursor3.getColumnIndex("title")));
mVal2 = mVal.toString();
mCursor3.close();
return;
}
} while (mCursor3.moveToNext());
}
mCursor3.close();
}
if i comment this code application print "School is off today. Reason: text
else if (message.equals("holiday"))
// {
// menu_nametxt.setText("No menu available .");
// listCategory.setVisibility(View.GONE);
// }
答案 0 :(得分:0)
由于第一次运行应用时message
为null
,当if-else语句到达else if (message.equals("holiday")
时,它会抛出NullPointerException
而不是{{} 1}}并崩溃了应用程序。
请考虑使用空字符串初始化false
。 (即message
)。
OR
将检查顺序更改为message = "";
。