I am receiving a NPE in my main classe although I have referenced all the values.
My code starts with:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, AbsListView.OnScrollListener {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
ListView listview;
final ArrayList<String> finalbuilder = new ArrayList<String>();
String[] Supermarkets = {"a", "b", "c", "d", "e"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.secondlayout);
RelativeLayout layout2 = (RelativeLayout) findViewById(R.id.thirdlayout);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
listview = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapterlistview = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, Supermarkets);
listview.setAdapter(adapterlistview);
and, by reading the stack trace, I see it all goes down to
expListView = (ExpandableListView) findViewById(R.id.lvExp);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.secondlayout);
RelativeLayout layout2 = (RelativeLayout) findViewById(R.id.thirdlayout);
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
Now, all these Views are contained in a different layout which is "first_fragment" or "second_fragment" rather than the "activity_main" declared in the OnCreate method. This is because I created a Navigation Drawer and the ExpandableListView is contained in the first Fragment, both ListView and secondlayout lie in the second fragment and thirdlayout is placed in the third Fragment's layout. Moreover, I set no null in the codes.
So my guess is that I cannot initialise something that is placed in a different layout from the one initiated with the OnCreate method. Hence I am wondering: does the use of Fragments pose any threats when working from the MainActivity on Views embedded in further layouts?
If so:
Should I simply add @Nullable to the OnCreate?
Should I Implement the FirstFragment while defining the class at the beginning?
Should I add "new" when declaring the ExpandableListView and the other Views at the beginning?