{"action":"created","issue":{"url":"https://api.github.com/repos/excilys/androidannotations/issues/1574","labels_url":"https://api.github.com/repos/excilys/androidannotations/issues/1574/labels{/name}","comments_url":"https://api.github.com/repos/excilys/androidannotations/issues/1574/comments","events_url":"https://api.github.com/repos/excilys/androidannotations/issues/1574/events","html_url":"https://github.com/excilys/androidannotations/issues/1574","id":109530392,"number":1574,"title":"Views are null at first @AfterViews in @PreferenceScreen @EActivity","user":{"login":"m-radzikowski","id":4042673,"avatar_url":"https://avatars.githubusercontent.com/u/4042673?v=3","gravatar_id":"","url":"https://api.github.com/users/m-radzikowski","html_url":"https://github.com/m-radzikowski","followers_url":"https://api.github.com/users/m-radzikowski/followers","following_url":"https://api.github.com/users/m-radzikowski/following{/other_user}","gists_url":"https://api.github.com/users/m-radzikowski/gists{/gist_id}","starred_url":"https://api.github.com/users/m-radzikowski/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/m-radzikowski/subscriptions","organizations_url":"https://api.github.com/users/m-radzikowski/orgs","repos_url":"https://api.github.com/users/m-radzikowski/repos","events_url":"https://api.github.com/users/m-radzikowski/events{/privacy}","received_events_url":"https://api.github.com/users/m-radzikowski/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":11,"created_at":"2015-10-02T16:55:33Z","updated_at":"2015-10-14T07:29:55Z","closed_at":null,"body":"I have very simple preference Activity:\r\n\r\n```java\r\n@EActivity(R.layout.a_settings)\r\n@PreferenceScreen(R.xml.preferences)\r\npublic class SettingsActivity extends PreferenceActivity {\r\n\r\n\t@ViewById(R.id.toolbar)\r\n\tprotected Toolbar toolbar;\r\n\r\n\t@PreferenceByKey(R.string.some_preference)\r\n\tprotected Preference somePref;\r\n\r\n\t@AfterViews\r\n\tprotected void setupToolbar() {\r\n\t\ttoolbar.setNavigationOnClickListener(new View.OnClickListener() {\r\n\t\t\t@Override\r\n\t\t\tpublic void onClick(View v) {\r\n\t\t\t\tonBackPressed();\r\n\t\t\t}\r\n\t\t});\r\n\t}\r\n```\r\n\r\nand it causes NPE on toolbar#setNavi...\r\n\r\nWhen i debugged it it turned out that `#setupToolbar()` method is in fact called 3 times and at first 2 times `toolbar` is `null`, at third time it's valid object.\r\n\r\nOf course workaround is simple - check if used views are not null:\r\n```java\r\n\t@AfterViews\r\n\tprotected void setupToolbar() {\r\n\t\tif (toolbar != null) {\r\n\t\t\ttoolbar.setNavigationOnClickListener(new View.OnClickListener() {\r\n\t\t\t\t@Override\r\n\t\t\t\tpublic void onClick(View v) {\r\n\t\t\t\t\tonBackPressed();\r\n\t\t\t\t}\r\n\t\t\t});\r\n\t\t}\r\n\t}\r\n```\r\n\r\nBut it looks like a bug - in `@AfterViews` method all views should be always already injected, right?"},"comment":{"url":"https://api.github.com/repos/excilys/androidannotations/issues/comments/147961884","html_url":"https://github.com/excilys/androidannotations/issues/1574#issuecomment-147961884","issue_url":"https://api.github.com/repos/excilys/androidannotations/issues/1574","id":147961884,"user":{"login":"WonderCsabo","id":480485,"avatar_url":"https://avatars.githubusercontent.com/u/480485?v=3","gravatar_id":"","url":"https://api.github.com/users/WonderCsabo","html_url":"https://github.com/WonderCsabo","followers_url":"https://api.github.com/users/WonderCsabo/followers","following_url":"https://api.github.com/users/WonderCsabo/following{/other_user}","gists_url":"https://api.github.com/users/WonderCsabo/gists{/gist_id}","starred_url":"https://api.github.com/users/WonderCsabo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WonderCsabo/subscriptions","organizations_url":"https://api.github.com/users/WonderCsabo/orgs","repos_url":"https://api.github.com/users/WonderCsabo/repos","events_url":"https://api.github.com/users/WonderCsabo/events{/privacy}","received_events_url":"https://api.github.com/users/WonderCsabo/received_events","type":"User","site_admin":false},"created_at":"2015-10-14T07:29:55Z","updated_at":"2015-10-14T07:29:55Z","body":"That is because the PreferenceActivity calls `setContentView()` [two](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/preference/PreferenceActivity.java#550) [times](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/preference/PreferenceActivity.java#633) with a different layout. We cannot do anything about that. Yeah, you have to use a `null` check, of course. But the view will be injected at last."}}