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

  • Home
  • SEARCH
  • 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 9145055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:25:09+00:00 2026-06-17T10:25:09+00:00

I am new to Android development and these forums, so I’d be grateful if

  • 0

I am new to Android development and these forums, so I’d be grateful if you could help. I’ve looked around and tried some other answers but can’t get anywhere.

I’ve written a Java method that returns random Strings from an ArrayList. I have three TextView elements on the main activity layout XML that I would like to set when a button is pressed (running the method to generate three random Strings).

How do I go about doing so, whenever I run the App on my phone, it crashes so there’s obviously a major flaw.

Here is the method I am calling

public void generateExercises() {


    Exercise e1 = new Exercise();
    Exercise e2 = new Exercise();
    Exercise e3 = new Exercise();

    e1.generateExercise();
    e2.generateExercise();
    e3.generateExercise();

    RelativeLayout lView = new RelativeLayout(this);


    myText1 = (TextView)findViewById(R.id.myText1);
    myText1.setText(e1.getName());

    myText2 = (TextView)findViewById(R.id.myText2);
    myText2.setText(e2.getName());

    myText3 = (TextView)findViewById(R.id.myText3);
    myText3.setText(e3.getName());

    lView.addView(myText1);
    lView.addView(myText2);
    lView.addView(myText3);     

    setContentView(lView);
}
//XML File

<TextView
    android:id="@+id/myText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/exercise2title"
    android:layout_alignBottom="@+id/exercise2title"
    android:layout_alignLeft="@+id/textView1"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/myText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/exercise3title"
    android:layout_alignBottom="@+id/exercise3title"
    android:layout_alignLeft="@+id/TextView01"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/exercise1title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/title"
    android:layout_marginLeft="39dp"
    android:layout_marginTop="64dp"
    android:text="@string/exercisetitle_one"
    android:textAppearance="?android:attr/textAppearanceMedium" />

Thanks

EDIT: Missed out the section of XML with “myText1”

 <TextView
    android:id="@+id/myText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/exercise1title"
    android:layout_alignBottom="@+id/exercise1title"
    android:layout_alignRight="@+id/title"
    android:layout_marginRight="24dp"
    android:textAppearance="?android:attr/textAppearanceSmall" />

EDIT: Thank you for all of your help, the code is now working as expected. I also had to replace the XML onclick method with a custom onClick listener.

  • 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-17T10:25:10+00:00Added an answer on June 17, 2026 at 10:25 am

    Call setContentView(lView) from your onCreate method.

    After that, you can get/set your textViews:

    myText1 = (TextView)findViewById(R.id.myText1);
    myText1.setText(e1.getName());
    

    You don’t need to use addView, because the views already exist in lView (you’ve set them up in the xml).

    So basically,

    Setting the contentView to lView will take your xml layout and create it with all the views. After that, you find the textview with myText1 = (TextView)findViewById(R.id.myText1); and then you set the text with myText1.setText(e1.getName());

    It’s also useful to view the logcat output to see what’s going wrong, and try the ‘debug’ feature of eclipse, with a breakpoint set on ‘nullPointerException’. Posting the logcat output here can make it 100x faster for us to find out what’s going wrong!

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

Sidebar

Related Questions

I'm new to Android development and was playing around with the camera. I just
I am very new to Android development and Java. Have read around but I'm
I'm new to Android development and I'm currently going through some tutorials. When I
I'm new in Android development and I have the next question/problem. I'm playing around
im fairly new to android development, but i'm having some trouble trying to make
I am brand new to Android development and have gotten through these two resources
I am brand new to Android development and have gotten through these two resources
I am new to Android development with Phonegap and just got through these two
I'm new to Eclipse and I'm using for some Android development. Eclipse is making
I am new to android app development so please any answers would be great

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.