-1

I've an android app, who send notifications every day once via NotificationActivity if User check a slider to enable notifications My problem is simple: when I launch NotificationActivity from MainActivity, app crashes

I've an android app, who send notifications every day once via NotificationActivity if User check a slider to enable notifications My problem is simple: when I launch NotificationActivity from MainActivity, app crashes Logs tells me that come from views that provides nullPointerException :

I think my views (editText, slider, checkboxes) that are in NotificationActivity are not correctly binded

error :

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.matt.android.mynews/com.matt.android.mynews.controllers.activities.NotificationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setSelection(int)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2721)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1521)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6228)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setSelection(int)' on a null object reference
    at com.matt.android.mynews.controllers.activities.NotificationActivity.initNotification(NotificationActivity.java:109)
    at com.matt.android.mynews.controllers.activities.NotificationActivity.configureDisplayAndPrefs(NotificationActivity.java:185)
    at com.matt.android.mynews.controllers.activities.NotificationActivity.onCreate(NotificationActivity.java:63)
    at android.app.Activity.performCreate(Activity.java:6860)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2674)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1521) 

setSelection(0) that provides NullPointerException is for EditText to open automatically keyboard If I remove it, exception come from next widget: checkbox, etc

NotificationActivity:

public class NotificationActivity extends AppCompatActivity {

    SharedPreferencesManager preferences;

    @BindView(R.id.search_query)
    EditText search_query;
    @BindView(R.id.switch_notification)
    Switch switchNotification;
    @BindView(R.id.travel_check_box)
    CheckBox travelCheckBox;
    @BindView(R.id.sports_check_box)
    CheckBox sportsCheckBox;
    @BindView(R.id.arts_check_box)
    CheckBox artsCheckBox;
    @BindView(R.id.politics_check_box)
    CheckBox politicsCheckBox;
    @BindView(R.id.entrepreneurs_check_box)
    CheckBox entrepreneursCheckBox;
    @BindView(R.id.business_check_box)
    CheckBox businessCheckBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);

        this.configureDisplayAndPrefs();
    }

    /**
     * Get and set toolbar
     */
    private void configureToolBar() {
        Toolbar toolbar = findViewById(R.id.toolbar_main_activity);
        setSupportActionBar(toolbar);

        //This is for back button
        //Get a support Action bar corresponding to above toolbar
        ActionBar actionBar = getSupportActionBar();
        //Enable the up button
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    /**
     * Check if the switch has been activated by the user and if so, get the last user input
     */
    private void isSwitchChecked(){

        boolean switchIsChecked = preferences.getBoolean(PREF_KEY_SWITCH);
        if (switchIsChecked) {
            //Set switch to checked
            switchNotification.setChecked(true);
            //Set search query
            search_query.setText(preferences.getString(PREF_KEY_QUERY));
            //Check the checkboxes that were selected
            artsCheckBox.setChecked(preferences.getBoolean(PREF_KEY_ARTS));
            politicsCheckBox.setChecked(preferences.getBoolean(PREF_KEY_POLITICS));
            entrepreneursCheckBox.setChecked(preferences.getBoolean(PREF_KEY_ENTREPRENEURS));
            sportsCheckBox.setChecked(preferences.getBoolean(PREF_KEY_SPORTS));
            travelCheckBox.setChecked(preferences.getBoolean(PREF_KEY_TRAVEL));
            businessCheckBox.setChecked(preferences.getBoolean(PREF_KEY_BUSINESS));

        }
    }

    /**
     * Set listener to switch to enable notifications
     */
    private void initNotification() {
        //Open the keyboard automatically
        search_query.setSelection(0);
        Objects.requireNonNull(this.getWindow()).setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

        //Set Listener to switch
        switchNotification.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //Get user input
                    preferences.getUserInput(search_query, null, null,
                            artsCheckBox, businessCheckBox, entrepreneursCheckBox,
                            politicsCheckBox, sportsCheckBox, travelCheckBox);

                    //If at least 1 checkbox is selected and user has put one search query --> enable notifications
                    if(preferences.checkConditions()) {
                        saveNotificationUrlAndState();
                        enableNotification();
                    } else {
                        preferences.clearInput();
                        Toast.makeText(getApplicationContext(), "Please select at least a categorie and a keyword", Toast.LENGTH_SHORT).show();
                        switchNotification.toggle();
                    }
                }
                //If switch is unchecked
                else {
                    cancelNotification();
                }
            }
        });
    }

    /**
     * Get the user input and save it in SharedPreferences
     */
    private void saveNotificationUrlAndState() {
        //Switch button
        preferences.putBoolean(PREF_KEY_SWITCH, true);
        //Edit hint text Text Query
        preferences.putString(PREF_KEY_QUERY, search_query.getText().toString());
        //CheckBoxes
        preferences.putBoolean(PREF_KEY_ARTS, artsCheckBox.isChecked());
        preferences.putBoolean(PREF_KEY_POLITICS, politicsCheckBox.isChecked());
        preferences.putBoolean(PREF_KEY_BUSINESS, businessCheckBox.isChecked());
        preferences.putBoolean(PREF_KEY_ENTREPRENEURS, entrepreneursCheckBox.isChecked());
        preferences.putBoolean(PREF_KEY_SPORTS, sportsCheckBox.isChecked());
        preferences.putBoolean(PREF_KEY_TRAVEL, travelCheckBox.isChecked());

        //Save search url
        preferences.createSearchUrlForAPIRequest();
        preferences.saveUrl(PREF_KEY_NOTIFICATION_URL);
    }

    /**
     * Use work manager to run a notification
     */
    private void enableNotification(){

    }

    /**
     * Cancel notification switch
     */
    private void cancelNotification(){
        preferences.clearInput();
        preferences.putBoolean(PREF_KEY_SWITCH, false);

        //Worker.cancel

        Toast.makeText(NotificationActivity.this, "Notifaction disabled", Toast.LENGTH_SHORT).show();

    }

    private void configureDisplayAndPrefs() {
        this.configureToolBar();
        preferences = new SharedPreferencesManager(this);
        this.isSwitchChecked();
        this.initNotification();
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
M.vacc
  • 43
  • 9

2 Answers2

1

Change your activity to

@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
ButterKnife.bind(this);
}

You forgot to bind the views.

Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
1

Not able to see the basic butterknife binding code in Oncreate() of activity

ButterKnife.bind(this);

Looks like the views are not binded properly