Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8761639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:20:06+00:00 2026-06-13T15:20:06+00:00

I have a layout called view.xml which contains a View called view_related . Basically,

  • 0

I have a layout called view.xml which contains a View called view_related. Basically, in my dictionary app, if there are related words to an entry, I will replace view_related with a LinearLayout that contains a header “Related entries” and a bunch of TextViews representing every related word.

I keep getting a NullPointerException everytime I’m adding a TextView to the LinearLayout in my Activity‘s onCreate() method, and I don’t understand why though my code looks pretty straightforward.

view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView android:id="@+id/view_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12pt"
        android:textColor="#fff"
        android:textStyle="bold"
        android:background="#990011"
        android:padding="10dp"
        android:text="lalala word"
        />

    <ScrollView android:id="@+id/view_description"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/view_header">
        <LinearLayout android:id="@+id/view_description_content"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="20dp"
                android:text="@string/demo_definition"
                />
                <!-- THIS WILL BE REPLACED -->
            <View android:id="@+id/view_related"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></View>
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

view_related.xml, the LinearLayout that will replace the View element in view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_related_entries"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView android:id="@+id/view_related_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textColor="#fff"
        android:textStyle="bold"
        android:textSize="7pt"
        android:background="#000"
        android:text="Related entries"
        />
    <LinearLayout android:id="@+id/view_related_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    </LinearLayout>
</LinearLayout>

And finally, the onCreate method, which for now assumes the related entries are “Hello” and “Goodbye:”

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view);

    LinearLayout ll = (LinearLayout) findViewById(R.id.view_related_list);
    TextView tv = new TextView(this);
    tv.setText("Hello");
    ll.addView(tv); // NULL POINTER EXCEPTION

    tv = new TextView(this);
    tv.setText("Goodbye");
    ll.addView(tv);

    LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = findViewById(R.id.view_related);
    ViewGroup parent = (ViewGroup) view.getParent();
    int index = parent.indexOfChild(view);
    parent.removeView(view);
    View llview = li.inflate(R.id.view_related_entries, parent, false);
    parent.addView(llview, index);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:20:07+00:00Added an answer on June 13, 2026 at 3:20 pm

    the line (LinearLayout ll = (LinearLayout) findViewById(R.id.view_related_list);) will search for view_related_list in view.xml, as in the line before that is the view you are using..

    You need to inflate view_related.xml first and then get the view from it..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an imageview in a layout called footer.xml which is actually added as
I have a layout setup which can be view here: http://jsfiddle.net/Pn3ts/ It all works
I have one custom ListView called AreasListView which I've included in the XML file.
I currently have a listview which contains a couple of strings. These are called
i have spinner with array that i create in a string.xml which called array.spinner_title
I have a layout like this in which I want to display a label
I have a layout which looks like this: <LinearLayout> <ImageView> android:width=wrap_content android:height:wrap_content ... </ImageView>
In ViewPagerAdapter.class I have 2 layout(whereami.xml and mapview.xml), and use LayoutInflater to inflate each
I want to use a list view and I have created my own layout
I have a custom view (which extends GLSurfaceView) I'm trying to use in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.