Android - View.post()不会从onCreate()修改TextView

时间:2021-04-23 17:36:15

Why does this not change my TextView's text ? I have a TextView who's text I try to modify from a view.post method. What am I doing wrong here ?

为什么这不会改变我的TextView文本?我有一个TextView,我尝试从view.post方法修改文本。我在这做错了什么?

mTextView = (TextView)findViewById(R.id.text_view);

new Thread(new Runnable() {
    @Override
    public void run() {
        mTextView.post(new Runnable(){
            @Override
            public void run() {
                mTextView.setText("A change in text, thank you very much");
            }
        });
    }
}).start();

This runs in the Activity's onCreate() method. However, if I define a handler and pass the Runnable to the handler the TextView is modified.

这在Activity的onCreate()方法中运行。但是,如果我定义一个处理程序并将Runnable传递给处理程序,则会修改TextView。

Edit: The code I have here is based on the example:

编辑:我这里的代码基于以下示例:

public void onClick(View v) {
    new Thread(new Runnable() {
        public void run() {
            final Bitmap bitmap =
                    loadImageFromNetwork("http://example.com/image.png");
            mImageView.post(new Runnable() {
                public void run() {
                    mImageView.setImageBitmap(bitmap);
                }
            });
        }
    }).start();
}

at http://developer.android.com/guide/components/processes-and-threads.html

Edit 2: Now I am totally confused. When I run the same code from an onClickListener attached to a Button in my Activity, the TextView's text is indeed manipulated. Why did this not happen in the onCreate method ?

编辑2:现在我完全糊涂了。当我从附加到Activity中的Button的onClickListener运行相同的代码时,TextView的文本确实被操作。为什么在onCreate方法中没有发生这种情况?

3 个解决方案

#1


In order to update a text view in activity you must use runOnUiThread

要更新活动中的文本视图,必须使用runOnUiThread

runOnUiThread(new Runnable() {
    @Override 
    public void run() { 
        mTextView.setText("A change in text, thank you very much"); 
    }
}); 

You can use With using AsycTask to load your Image in a Imageview

您可以使用With使用AsycTask在Imageview中加载图像

public class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {

    private String url;
    private ImageView imageView;

    public ImageLoadTask(String url, ImageView imageView) {
        this.url = url;
        this.imageView = imageView;
    } 

    @Override 
    protected Bitmap doInBackground(Void... params) {
        try { 
            URL urlConnection = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) urlConnection
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return null; 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        imageView.setImageBitmap(result);

       // you can also update your textview here 

    } 

} 

And Call this Task On Your Onclick

并在Onclick上调用此任务

button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // pass your url and your imageview 
                // eg :http://example.com/image.png
                // imageView=(your ImageView)
                   new ImageLoadTask(url, imageView).execute();
            }
        });

#2


Why you are running your code inside another thread? all the UI operations works only in the UI thread..try executing only the below code

为什么要在另一个线程中运行代码?所有UI操作仅在UI线程中起作用..仅执行以下代码

mTextView = (TextView)findViewById(R.id.text_view);
mTextView.post(new Runnable() {
    @Override
    public void run() {
        mTextView.setText("A change in text, thank you very much");
    }
});

#3


Just run without new Thread(new Runnable()

只运行没有新的线程(新的Runnable()

     mTextView.post(new Runnable(){

         @Override
         public void run() {

             // anything you want
         }
     });

#1


In order to update a text view in activity you must use runOnUiThread

要更新活动中的文本视图,必须使用runOnUiThread

runOnUiThread(new Runnable() {
    @Override 
    public void run() { 
        mTextView.setText("A change in text, thank you very much"); 
    }
}); 

You can use With using AsycTask to load your Image in a Imageview

您可以使用With使用AsycTask在Imageview中加载图像

public class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {

    private String url;
    private ImageView imageView;

    public ImageLoadTask(String url, ImageView imageView) {
        this.url = url;
        this.imageView = imageView;
    } 

    @Override 
    protected Bitmap doInBackground(Void... params) {
        try { 
            URL urlConnection = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) urlConnection
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return null; 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        imageView.setImageBitmap(result);

       // you can also update your textview here 

    } 

} 

And Call this Task On Your Onclick

并在Onclick上调用此任务

button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // pass your url and your imageview 
                // eg :http://example.com/image.png
                // imageView=(your ImageView)
                   new ImageLoadTask(url, imageView).execute();
            }
        });

#2


Why you are running your code inside another thread? all the UI operations works only in the UI thread..try executing only the below code

为什么要在另一个线程中运行代码?所有UI操作仅在UI线程中起作用..仅执行以下代码

mTextView = (TextView)findViewById(R.id.text_view);
mTextView.post(new Runnable() {
    @Override
    public void run() {
        mTextView.setText("A change in text, thank you very much");
    }
});

#3


Just run without new Thread(new Runnable()

只运行没有新的线程(新的Runnable()

     mTextView.post(new Runnable(){

         @Override
         public void run() {

             // anything you want
         }
     });