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 4027830
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:08:43+00:00 2026-05-20T11:08:43+00:00

Im trying to add a textview to a linear layout progmmatically, but I keep

  • 0

Im trying to add a textview to a linear layout progmmatically, but I keep getting a null pointer on my linear layout. Its being called after the layout is inflated, so I do not understand why its happening. Heres the code.

tile_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:id="@+id/master"
android:orientation="horizontal"    
android:layout_width="fill_parent"   
android:layout_height="match_parent"> 
<TextView
android:id="@+id/fragment_two_directory"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Testing Fragment Two" />



</LinearLayout>

fragment_two.java

public class fragment_two extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.tile_layout,container, false);
}

public static fragment_two newInstance(String directory, Activity activity) {
    fragment_two ft = new fragment_two();
    TextView text = (TextView)activity.findViewById(R.id.fragment_two_directory);
    text.setText(directory);
    return ft;
}
public static void setDirectoryText(String directory, Activity activity) {
    TextView directoryText = (TextView)activity.findViewById(R.id.fragment_two_directory);
    directoryText.setText(directory);
    populateDirectory(directory, activity);
}
public static void populateDirectory(String directory, Activity activity) {
    LinearLayout masterLinear = (LinearLayout)activity.findViewById(R.id.master);
    TextView testText = new TextView(activity);
    testText.setText("test");
    masterLinear.addView(testText);
    //testText.setText("Testing Add View");
    //for (int x=0; x < 5; x++) {
        //FrameLayout container = new FrameLayout(getActivity());
        //container.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        //ImageView fileImage = new ImageView(getActivity());
        //fileImage.setLayoutParams(new FrameLayout.LayoutParams(110, 110));
        //fileImage.setImageResource(R.drawable.blank_file);
        //container.addView(fileImage);
        //TextView fileName = new TextView(getActivity());
        //fileName.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        //fileName.setText("Filename.txt");
        //container.addView(fileName);
        //masterLinear.addView(container);
    //}

}
}

And finally here is the main activity

package com.bv.dual_fragments;

import com.bv.dual_fragments.fragment_one.OnArticleSelectedListener;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

    public class dual_fragments extends Activity implements OnArticleSelectedListener {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        //((global_holder)this.getApplication()).setDirectory("test"); 

    }
    @Override
    public void onArticleSelected(String directory) {
        //fragment_two frag_two = (fragment_two)getFragmentManager().findFragmentById(R.id.container);
        //TextView containerText = (TextView)findViewById(R.id.fragment_two_directory);
        //containerText.setText(directory);
        fragment_two.populateDirectory(directory, this);
    }
}

Heres the LogCat

03-06 17:35:47.452: ERROR/AndroidRuntime(3576): FATAL EXCEPTION: main
03-06 17:35:47.452: ERROR/AndroidRuntime(3576): java.lang.NullPointerException
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at     com.bv.dual_fragments.fragment_two.populateDirectory(fragment_two.java:36)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at com.bv.dual_fragments.dual_fragments.onArticleSelected(dual_fragments.java:25)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at com.bv.dual_fragments.fragment_one.onListItemClick(fragment_one.java:88)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.app.ListFragment$2.onItemClick(ListFragment.java:160)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.widget.AdapterView.performItemClick(AdapterView.java:282)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.widget.AbsListView.performItemClick(AbsListView.java:1032)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2447)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3077)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.widget.ListView.onTouchEvent(ListView.java:3566)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.View.dispatchTouchEvent(View.java:4600)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1488)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1256)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1494)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1269)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1700)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1270)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.app.Activity.dispatchTouchEvent(Activity.java:2271)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1680)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2272)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1958)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.os.Looper.loop(Looper.java:126)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at android.app.ActivityThread.main(ActivityThread.java:3997)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at java.lang.reflect.Method.invoke(Method.java:491)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-06 17:35:47.452: ERROR/AndroidRuntime(3576):     at dalvik.system.NativeStart.main(Native Method)

I also ran this in the honeycomb emulator, and looked at the heirarchy viewer. I have two linearlayout inside of that layoutfile, and neither of them are showing up, only the textview.

  • 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-05-20T11:08:44+00:00Added an answer on May 20, 2026 at 11:08 am

    Ok, the problem was the layout file was messed up and I had two linear layouts going horizontaly, and therefore the heirarchy wouldnt load them, giving the null point exception. Just a tip for anyone looking at the heirarchyviewer for Android 3.0, the fragment (inside the layoutfile) will automatically take ownership of the first layout tag in its inflated view. So say you have

    <fragment android:id="@+id/viewer" />
    

    and in your layout file you have

    <LinearLayout android:id="@+id/first_layout" />
    

    In heirarchy viewer, the LinearLayout will appear as viewer, not first_layout, since the fragment doesnt have a default layout, its going to re-id the first layout container in its layoutfile to its own id.

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

Sidebar

Related Questions

I am trying to parse a URL in Android but it gives a null
I'm trying to replicate Apple's layout of its app icons. I'm having trouble using
I am trying to add a line break in the TextView. I tried suggested
I'm trying to add a complex / non-trivial header to a ListView (and slightly
iam trying to place admob add at bnottom of my application ...for that i
I am trying to add rows in a TableLayout programmatically and I am following
I'm trying to get a row of text which will be something like foofoofoo
in my app i am trying to show two buttons and an a title
Edit: Lesson learned: set the orientation or stuff might not show up. As per

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.