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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:14:52+00:00 2026-06-08T05:14:52+00:00

I am trying to add a TextView dynamically into a layout which is part

  • 0

I am trying to add a TextView dynamically into a layout which is part of a Fragment, but it is not working. As soon as the application starts it shuts down immediately.
I think it has to do something with the
TextView Paper = new TextView(this); part. I dont know how to get the right Context since I’m working with Fragments. Maybe my assumption is wrong but I really dont know where the error might be.

public class PageFragment extends Fragment {

    private int fragmentNR;

    public PageFragment(int nr) {
        this.fragmentNR = nr;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = new View(getActivity());
        if (fragmentNR == 0)
            v = inflater.inflate(R.layout.page1, container, false);
        else if (fragmentNR == 1)
            v = inflater.inflate(R.layout.page2, container, false);
        else if (fragmentNR == 2)
            v = inflater.inflate(R.layout.page3, container, false);

        return v;
    }

}
public class MainActivity extends FragmentActivity  {

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

      PageAdapter mPageAdapter = new PageAdapter(getSupportFragmentManager());
      ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager);
      mViewPager.setAdapter(mPageAdapter);

      //This is where i try to add my TextView into my Layout
      LinearLayout PaperLayout = (LinearLayout) findViewById(R.id.PaperLayout);//PaperLayout is a LinearLayout within the Page2-Fragment(R.layout.page2)
      TextView Paper = new TextView(this);
      Paper.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
      Paper.setText("Paper2");
      PaperLayout.addView(Paper);
    }        
}

logcat

07-18 16:32:58.330: E/AndroidRuntime(614): FATAL EXCEPTION: main
07-18 16:32:58.330: E/AndroidRuntime(614): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.loni.math_prime/com.loni.math_prime.MainActivity}: java.lang.NullPointerException
07-18 16:32:58.330: E/AndroidRuntime(614):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-18 16:32:58.330: E/AndroidRuntime(614):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-18 16:32:58.330: E/AndroidRuntime(614):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-18 16:32:58.330: E/AndroidRuntime(614):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-18 16:32:58.330: E/AndroidRuntime(614):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 16:32:58.330: E/AndroidRuntime(614):  at android.os.Looper.loop(Looper.java:137)
07-18 16:32:58.330: E/AndroidRuntime(614):  at android.app.ActivityThread.main(ActivityThread.java:4745)
07-18 16:32:58.330: E/AndroidRuntime(614):  at java.lang.reflect.Method.invokeNative(Native Method)
07-18 16:32:58.330: E/AndroidRuntime(614):  at java.lang.reflect.Method.invoke(Method.java:511)
07-18 16:32:58.330: E/AndroidRuntime(614):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-18 16:32:58.330: E/AndroidRuntime(614):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-18 16:32:58.330: E/AndroidRuntime(614):  at dalvik.system.NativeStart.main(Native Method)
07-18 16:32:58.330: E/AndroidRuntime(614): Caused by: java.lang.NullPointerException
07-18 16:32:58.330: E/AndroidRuntime(614):  at com.loni.math_prime.MainActivity.onCreate(MainActivity.java:30)
07-18 16:32:58.330: E/AndroidRuntime(614):  at android.app.Activity.performCreate(Activity.java:5008)
07-18 16:32:58.330: E/AndroidRuntime(614):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-18 16:32:58.330: E/AndroidRuntime(614):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-18 16:32:58.330: E/AndroidRuntime(614):  ... 11 more

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
     >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <android.support.v4.view.PagerTitleStrip
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="32dp"
            android:textStyle="bold"
            android:textColor="#fff5ee"
            android:layout_gravity="top" />
    </android.support.v4.view.ViewPager>

</LinearLayout>
  • 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-08T05:14:55+00:00Added an answer on June 8, 2026 at 5:14 am

    Solved:

    public class PageFragment extends Fragment {
    
    private int fragmentNR;
    
    
    public PageFragment(int nr) {
        this.fragmentNR = nr;
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
        View v = new View(getActivity());
    
        if (fragmentNR == 0)
            v = inflater.inflate(R.layout.page1, container, false);
        else if (fragmentNR == 1){
            v = inflater.inflate(R.layout.page2, container, false);
            View tv = v.findViewById(R.id.Ausgabe);
            ((TextView)tv).setText("TestText");
            View pl = v.findViewById(R.id.PageLayout);
            TextView Paper = new TextView(pl.getContext());
            Paper.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            Paper.setText("Inserted TestText");
            ((LinearLayout)pl).addView(Paper);
            }
        else if (fragmentNR == 2)
            v = inflater.inflate(R.layout.page3, container, false);
    
        return v;
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to add a textview to a linear layout progmmatically, but I keep
I am trying to add a textview inside a tab dynamically. using this code
So i am trying to add TextView's into my main ListView within JAVA, And
I am trying to add a second image into my custom dialog but I
I am trying to add a Button in a LinearLayout after a TextView but
I am trying to dynamically add radiobuttons to radiogroups but the buttons never show
I am trying to dynamically add textviews to a linearLayout, say the textview is
I'm trying to add a textView to a frameLayout . The TextView has wrap_content
Hi in my app when i was trying to add a link in textview
I'm trying to add a bunch of TextViews at runtime to a scrollview but

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.