Android if and else if statement not working

2019-03-04 13:42发布

问题:

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?