0

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?

Marco De Roni
  • 37
  • 1
  • 8
  • You should use the inflater to get the view you need from the proper layout. usually you dont need to do something like this... – Nanoc Nov 12 '15 at 12:43
  • Hi Nanoc, how and why should I inflate all the views from x Fragments when opening the activity? Or should I inflate the ExpandableListView, ListView and layouts when the user clicks on the navigation drawer item? – Marco De Roni Nov 12 '15 at 14:00
  • You need to call findViewById from the View that contains the view you are looking for thats all, but i cant find an example when you were forced to get a reference to a view that isnt even in the screen at the time, can you explain why you need to do that? – Nanoc Nov 12 '15 at 15:33
  • Hi Nanoc, do you mean, call findViewById from the "Layout" that contains the view? If that's the case, the first fragment class can only implement Fragment (and not Activity). So that, instead of "public class FirstFragment extends Fragment, AppCOmpatActivity" I can only choose "public class FirstFragment extends Fragment" (see Diamond problem of Java structure). Therefore, without the Activity/AppCompatActivity I can't use any mehtod like findViewById and I need to develop everything from the MainActivity.class. Did I get your question correctly? – Marco De Roni Nov 12 '15 at 15:38
  • You have a wrong concept, in a Activity you first call setContentView to set the layout, then when you call findViewById it looks on that previously set layout, When in a Fragment, you NEED to use a Inflater in onCreateView to load an xml into a view, then you call findViewById from that inflated view. Anyway, where have you seen a class that implements both fragment and Activity?? – Nanoc Nov 12 '15 at 15:44
  • but without extending Activity nor AppCompatActivity the findViewByI does not work and everything gets highlighted in red, or am I wrong? – Marco De Roni Nov 12 '15 at 15:46
  • You are wrong, as i said findViewById is on the Activity class, but also in the ViewGroup class you use the activity where in an Activity, you use it from ViewGroup where in a fragment. doesnt make sense implementing both Activty or Fragment, a fragment must be attached to an activity and its designed to have multiple interchangeable fragments inside one single activity. – Nanoc Nov 12 '15 at 15:48
  • Not sure I grasp all but perhaps should I have: FirstFragment.class associated with firstfragment.xml and MainActivity.class associated with mainactivity.xml + public class FirstFragment extends Fragment implements MainActivity + public class MainActivity extends Activity implements FirstFragment, correct? – Marco De Roni Nov 12 '15 at 16:16
  • Thats still implementing both fragment and Activity... why, really why do you want to do that?? – Nanoc Nov 12 '15 at 16:21
  • Nanoc, I confirm that findViewById does not work if i only implement Fragment. It turns red and I need a qualifier for it – Marco De Roni Nov 12 '15 at 18:01
  • Yea you need to call it from the view you have just inflated, i told you. – Nanoc Nov 13 '15 at 08:38
  • just solved, Nanoc, thanks! – Marco De Roni Nov 13 '15 at 10:22

0 Answers0