title: How can I create a TextView programmatically in a Linear Layout?
body: <p>I'm trying to make a TextView programmatically in a LinearLayout. The program includes a checking system to check if its been added already and the prompt for creating the textview is an option in a spinner. Here is the full onClick method for the spinner</p>
  
  <pre><code>public void onClick(String Ingredient, int i) {
  
              Toast.makeText(Kitchen.super.getContext(), "Selected "+Ingredient, Toast.LENGTH_SHORT).show();
  
              if(Ingredient.equals(tomatoSauce.name)) {
  
                  if (tomatoSauce.init == 0){
  
                      tomatoSauce.init = 1;
                      TextView one = new TextView(getContext());
                      one.setText(Ingredient);
                      mainll.addView(one);
  
                  }
  
  
              } else if(Ingredient.equals(chicken.name)) {
  
                  chicken.init = 1;
  
              } else if(Ingredient.equals(olives.name)){
  
                  olives.init  = 1;
  
              }
  
          }
  </code></pre>
  
  <p>The Linear layout is identified from the xml layout when the app is started in a separate method.</p>
  
  <pre><code>final LinearLayout mainll = (LinearLayout) getActivity().findViewById(R.id.main);
  </code></pre>
  
  <p>The app crashes upon selecting Tomato Sauce from the menu despite the lack of identified coding errors. Any help with this issue would be greatly appreciated. </p>