-1

I am attempting to dynamically add FrameLayouts inside a RelativeLayout but am getting the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference

which occurs during rl.addView(fl)

public class StartingPage extends AppCompatActivity {

//set to 1 temporarily
int count = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_starting_page);

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainMenu);
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100, Gravity.CENTER_HORIZONTAL| Gravity.CENTER_VERTICAL);

    for(int i=0; i < count; i++){  
       FrameLayout fl = new FrameLayout(this);
       fl.setLayoutParams(params);

       TextView item = new TextView(this);
       fl.addView(item);

       rl.addView(fl);
    }
}

}

My research seems to conclude that this would work, why is the FrameLayout fl null? I understand that a LayoutInflater is an option, which I have briefly tried to no avail, but it seems that many people were simply able to create the layouts and views and add them to the desired parent in this simpler manner.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@+id/mainMenu"
    tools:context="com.marcel.joseph.project.Main_Menu">

</RelativeLayout>

Furthermore, what makes a layout null? Is it the lack of children, undefined attributes, or something else?

Thanks

  • 1
    Check your `RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainMenu);` line. I suspect the reference to the `RelativeLayout` is null. Post your XML as well to verify. – Clinkz Nov 22 '16 at 04:05
  • 1
    Is your `RelativeLayout` actually called `R.id.mainMenu`? Have you inflated the layout before calling `findViewById()`? – Ishita Sinha Nov 22 '16 at 04:15
  • I added the XML, the ID of the relative layout is mainMenu. However, I did not inflate the layout before calling findViewById(). Is that necessary in every case? – Joe Fortman Nov 22 '16 at 04:22
  • Put your entire code for onCreate() method of activity, as this seems you are not calling relative layout properly and the instance of relative layout is null – Vickyexpert Nov 22 '16 at 04:45
  • To answer your question, `what makes a layout null?` Its the inability to find the view on the layout attached. – Clinkz Nov 22 '16 at 04:50
  • Post the code of class – avinash Nov 22 '16 at 04:54
  • @JoeFortman : make sure you have called setContentView(R.layout.your_layout_name); before RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainMenu); – avinash Nov 22 '16 at 05:02
  • @avinash Possible solution. Albeit, that would have resulted in a null pointer at `fl.setLayoutParams(params);` – Clinkz Nov 22 '16 at 05:08
  • @JoeFortman : Try to clean and re build the current code what you have posted. And install the apk. I copied your code and installed its not crashing on my machine. – avinash Nov 22 '16 at 05:11
  • @JoeFortman You are adding null textview object in Fragment Layout.You should set layout prams to textview also. – Anil Ravsaheb Ghodake Nov 22 '16 at 05:12
  • @avinash it worked! Adding setContentView(...) that is. I read somewhere that findViewById() can only find child views, by setting the content view to the layout did that make the root layout available? – Joe Fortman Nov 22 '16 at 05:16
  • @JoeFortman : Yeah somewhat . setContentView(R.layout.main) to set xml layout to that activity which will actually render as the UI of your activity. As per documentation : An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). – avinash Nov 22 '16 at 05:33

2 Answers2

1

Check with below code:-I think you are not set layout prams to Textview because of that it gives error.

        public class StartingPage extends AppCompatActivity {

        //set to 1 temporarily
        int count = 1;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_starting_page);

            RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainMenu);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100, Gravity.CENTER_HORIZONTAL| Gravity.CENTER_VERTICAL);

            for(int i=0; i < count; i++){  
               FrameLayout fl = new FrameLayout(this);
               fl.setLayoutParams(params);

               TextView item = new TextView(this);
            RelativeLayout.LayoutParams objParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);

    item.setLayoutParams(objParams);
item.setText("test");
               fl.addView(item);

               rl.addView(fl);
            }
        }

        }

hope it helps you.Let me know if gets any error in code.

Anil Ravsaheb Ghodake
  • 1,587
  • 2
  • 27
  • 45
0

You need to initialize your FrameLayout first.Something like this

// Initialize a new FrameLayout
 FrameLayout fl = new FrameLayout(this);

Then you can Create Layout Parameters for FrameLayout.

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100, Gravity.CENTER_HORIZONTAL| Gravity.CENTER_VERTICAL);

One thing i am not clear that why your xml file name is mainMenu?are you try to use a menu resource file?

Real73
  • 490
  • 4
  • 13
  • Context provided by the user is that of the activity, how does providing the context of the application would make the initialization any different? – Clinkz Nov 22 '16 at 04:51
  • I am initializing the FrameLayout in a similar way, I use "this" as the context. As far as I'm aware that should be sufficient. But no I am not using a menu resource file, really am just experimenting to teach myself, so the naming wasn't done with any definite functionality in mind. – Joe Fortman Nov 22 '16 at 04:57