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.
Call setContentView(lView) from your onCreate method.
After that, you can get/set your textViews:
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 withmyText1.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!