4

I'm having a slight problem trying to display a variable on-screen.

Right now I have a tabbed layout. In the fourth tab there is a nested ActivityGroup which has a mapactivity inside of it.

The trouble is this map activities setContentView is set to R.layout.gps

I am trying to display the distance traveled on-screen ontop or beside the map to show how far the user has travelled.

I can get text to display on-screen by creating a text view inside of the XML. However using this method I am unable to link it to a Double variable inside of the activity.

At the same time if I create a TextView t = new TextView(this) it will not be displayed on-screen as the set content is now on it.

If anyone could shed any light on this it would be greatly appreciated.

2 Answers 2

5

Make sure that the TextView you want to use in res/layout/gps.xml has the following in it:

android:id="@+id/mytextview"

And then get use Klaus's code to find your TextView in your Java code:

TextView myTextView = (TextView) findViewById(R.id.mytextview); myTextView.setText("My double value is " + doubleValue);

0

Let's assume you have a TextView in your XML with the id of mytextview.

Use:

TextView myTextView = (TextView) findViewById(R.id.mytextview);

to get a dynamic reference to the TextView. Then you can use

myTextView.setText("My double value is " + doubleValue);

to update it as you go along.

1
  • I tried to do that as well but I got this error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.carbonaware.gps/com.carbonaware.gps.FirstGroup}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.carbonaware.gps/com.carbonaware.gps.gps}: java.lang.NullPointerExcep....... but it will work if I don't use the method .setText which has me confused
    – Patrick
    Mar 2, 2011 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.