Reputation: 3
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.
Upvotes: 0
Views: 128
Reputation: 26
Where is your myText1 in the XML?
myText1 = (TextView)findViewById(R.id.myText1);
myText1.setText(e1.getName()); //NPE here?
Upvotes: 0
Reputation: 6221
I guess you need to set the it to Contentview befor you add the Text to the TextViews. You try to add text to the TextView which are not created yet.
Just try do set contentView before you add the text to it. If i know it right, you do not need to add the TextViews to the lView because you actually did it in the XML file. You actually did define the Layout in the XML so you dont need to create it in the code again. Simply create the layout in the XML and fill it as you do right now.
public void generateExercises() {
Exercise e1 = new Exercise();
Exercise e2 = new Exercise();
Exercise e3 = new Exercise();
e1.generateExercise();
e2.generateExercise();
e3.generateExercise();
setContentView(R.layout.*****); //putyour layoutname here
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());
}
this schould do what you want to do
Upvotes: 1
Reputation: 6353
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!
Upvotes: 0
Reputation: 4427
public void generateExercises() {
setContentView(R.layout.xml_file_name);
Exercise e1 = new Exercise();
Exercise e2 = new Exercise();
Exercise e3 = new Exercise();
e1.generateExercise();
e2.generateExercise();
e3.generateExercise();
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());
}
Upvotes: 0