Android if and else if statement not working

风格不统一 提交于 2020-01-22 03:08:29

问题


I'm trying to use the following code to show one intent if the record count is = too or greater then 5 and a different intent if the record count is less then 5 on the first button of a dashboard layout.

But I keep getting the evil NullPointError..........Am I on the right track or am I flapping in the wind, please help and point out what i'm doing wrong...Thanks

private class DashboardClickListener implements OnClickListener {
   // @Override
    public void onClick(View v) {
        Intent i = null;
        switch (v.getId()) {
            case R.id.dashboard_button_one:
                if (mDbHelper.fetchAllNotes().getCount() >= 5){
                i = new Intent(DashboardActivity.this, Demo.class);} 
                else if (mDbHelper.fetchAllNotes().getCount() < 5){
                i = new Intent(DashboardActivity.this, one.class);}
                    break;
            case R.id.dashboard_button_two:
                i = new Intent(DashboardActivity.this, two.class);             
                break;
            case R.id.dashboard_button_three:
                i = new Intent(DashboardActivity.this, three.class);                   
                break;
            case R.id.dashboard_button_four:
                i = new Intent(DashboardActivity.this, four.class);                   
                break;
            default:
                break;
        }
        if(i != null) {
            startActivity(i);
        }
    }
}

回答1:


Did you instantiate your mDbHelper? Open the DB?



来源:https://stackoverflow.com/questions/10667585/android-if-and-else-if-statement-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!