开发者

Recognize textview in another class defined in the same Activity - Android

I have a class called Main that extends Activity. In this class I have defined another class that extends thread. I want to set text in a textView from a method of the myThread class. I have debugged my programme but when I want to set the text in the textview it says that the source is not found.

Here is my code. I hope you will understand better what I want to explain.

This happens when I click a button. This is from my onCreate function in the Main class.

  b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {    
            pd = ProgressDialog.show(Main.this, "", "Downloading Data...", true, false);
            MyThread mThread = new MyThread();
            mThread.start();

                 }
    });   

This is myThread class.

            class MyThread extends Thread {
            @Override
            public void run(){

                 String result=Main.this.parse();
                 TextView tv=(TextView) findViewById(R.id.textView5);
                 tv.setText(result);
                 Main.this.pd.dismiss();
            }
        }

The result String is a string that is resturned by a function that is defined in my Main class. I have verified and the result is OK. The programme stops when I reach the line

       tv.setText(result);

The odd thing is that it doesn't have any errors at line

        TextView tv=(TextView) findViewById(R.id.textView5);

Thank you for any help given! :D

       @Override
       public void onCreate(Bundle savedInstanceState) {
开发者_运维百科       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);   

    tv=(TextView) findViewById(R.id.textView5);
    Button b=(Button) findViewById(R.id.button1);
    final EditText et1=(EditText) findViewById(R.id.editText1);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {    
            pd = ProgressDialog.show(Main.this, "", "Downloading Data...", true, false);
            Log.d("asinc","inainte d downloadtask");
            // Start a new thread that will download all the data
            MyThread mThread = new MyThread();
            mThread.start();
            //parse();   
                 }
    });   


Maybe I miss soomething, but why you access UI components from non-main thread? As far as I understand UI is not thread safe; and so I would suggest use AsyncTask instead of Thread. In that case you can place your main proessing code into doInBackground method and all the UI component access into onPostExecute (onPostExecute is called from main thread after the end of task execution).


try to set in your main class into the onCreate()

private TextView tv; //declare as global so you can used into the subclass or into the methods

onCreate(){
   tv=(TextView) findViewById(R.id.textView5);
}

and then used into the thread

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜