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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:11:37+00:00 2026-05-25T21:11:37+00:00

I am a beginner of android, and was studying the layout. What i am

  • 0

I am a beginner of android, and was studying the layout.

What i am trying to do is i am trying to add a textView to a LinearLayout through code, and the application stops with the error that the application has stopped unexpectedly. The code is:

    setContentView(R.layout.main);

    LinearLayout abc = (LinearLayout)findViewById(R.id.cLayout);

    TextView tv = new TextView(this);
    tv.setText("Text Changed!!!!");

    abc.addView(tv);

And in the xml, id is set like: android:id=”@+id/cLayout”.

I know there are better ways to do it, but i want to know why this doesn’t work. Contents of textView are changing just fine. Also i have also tried

    LinearLayout abc = (LinearLayout)findViewById(R.layout.main);

What am i doing wrong? One thing is clear that i am accessing uncreated items (unassigned pointer).

Edit
Main xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/cLayout"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello World"
    android:id="@+id/myTV"
    />
</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-05-25T21:11:38+00:00Added an answer on May 25, 2026 at 9:11 pm

    I’ve tried to reproduce it, and it works for me, no exceptions.

    main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/cLayout"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
        <TextView  
              android:layout_width="fill_parent" 
              android:layout_height="wrap_content" 
              android:text="Hello World, MyActivity"/>
    </LinearLayout>
    

    MyActivity.java:

    public class MyActivity extends Activity
    {
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            LinearLayout ll = (LinearLayout) findViewById(R.id.cLayout);
    
            TextView tv = new TextView(this);
            tv.setText("Hello Again!");
    
            ll.addView(tv);
        }
    }
    

    UPD When I posted your code, I got an exception, too. Make sure you call super.onCreate().

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

Sidebar

Related Questions

I'm a beginner android developer , I was trying to run this Linear Layout
I am a beginner in Android. I have developed a application that will send
I am a beginner Android developer. I am trying to create an SMS application.
I'm a beginner android programmer who is trying to set up some USB functionality
I am implementing a Database for my (beginner/Android) application. It is not clear to
Beginner in Android development. My code crashes. I have made a simple Java method
I'm beginner in android and I need to create own QR code scanner app.
I am a beginner android developer and I am trying to have a button
I'm a beginner in Android. Here I have this code: public class GPS extends
Beginner Android and Java developer here. I have this snippet of code inside a

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.