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.