1. Check out our companion app, Forums for Android! Download from Google Play

Problem with accessing a textview

Discussion in 'Android Apps & Games' started by shirkkan, Mar 22, 2011.

  1. shirkkan

    shirkkan New Member
    Thread Starter
    5

    Oct 31, 2010
    3
    0
    5
    Hello!

    I'm developing an app that it throws an error when I try to assign a value to a textview. The structure is this:
    Code (Text):
    1. main class{
    2.    public Textview mytv;
    3.    onCreate{
    4.        mytv = (TextView) findViewById(R.id.Time);
    5.        mytv.setText("some text");
    6.    }
    7.  
    8.    public update(){
    9.        [U]mytv.setText("something");[/U]
    10.    }
    11.  
    12. }
    When I execute it, it gives me this error:

    WARN/System.err(733): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. (it refers to the underlined line)

    Why can't I set a value to that textview although it is declared as public???
    I've tried creating another instance of it inside the update function but also crashes with the same error at the same line.

    Where is the problem? Thanks!!
     

    Advertisement

  2. alostpacket

    alostpacket Over Macho Grande?
    513

    Nov 29, 2009
    7,978
    3,606
    513
    Android App Developer
    NY
    You need to call

    setContentView(R.layout.my_activity_layout);

    before

    findViewById(R.id.my_text_view_id);

    where my_activity_layout.xml is your layout for your activity



    This is assuimg your class is the Activity where this TextView is. If it's not, you need to have the Activity that holds the textview modify the text. Because you're not allowed to modify views from outside the Activity that created them.
     

Share This Page

Loading...