Android后台线程无法通过处理程序更改textview

时间:2013-07-27 12:06:41

标签: android

我无法理解为什么我的后台线程在更改屏幕方向后无法更改textView( txtName )的内容。

如果我将 txtname 设为静态,则可以正常工作,但如果没有静态则不起作用。它的初始值不会被后台线程更新。

private TextView txtName;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen2);

    txtName = (TextView) findViewById(R.id.txtName);
    TextView txtEmail = (TextView) findViewById(R.id.txtEmail);
    Button btnClose = (Button) findViewById(R.id.btnClose);

    Button btnBack = (Button) findViewById(R.id.btnBack);

    Intent i = getIntent();
    // Receiving the Data
    String name = i.getStringExtra("name");
    String email = i.getStringExtra("email");
   // String data = i.getStringExtra("data");
    // Displaying Received data
    txtName.setText("HI");
    txtEmail.setText(email);

    // Binding Click event to Button
    btnClose.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
              //Closing SecondScreen Activity
              Thread background = new Thread(new Runnable() {
                   @Override
                   public void run() {
                    for (int i = 0; i < 30; i++) {
                     try {
                      Thread.sleep(1000);
                      Message msg = new Message();
                      Bundle b = new Bundle();
                      b.putString("My Key", "My Value: " + String.valueOf(i));
                      msg.setData(b);
                      // send message to the handler with the current message handler
                      handler.sendMessage(msg);
                      Log.e("Error", "IN THREAD");
                      } catch (Exception e) {
                      Log.d("Error", e.toString());
                     }
                    }
                   }
              });
              background.start();
        }
    });


    btnBack.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
             Intent returnIntent = getIntent();
             returnIntent.putExtra("returnResult","i want to back page.");
             setResult(RESULT_OK,returnIntent);
             finish();

        }
    });



}








Handler handler = new Handler() {

      public void handleMessage(Message msg) {
       // get the bundle and extract data by key
       Bundle b = msg.getData();
       String key = b.getString("My Key");
       txtName.setText( "Item " + key);
       txtName.setVisibility(View.VISIBLE);
       Log.e("TEST MESSAGE",  txtName.getText().toString());
      }

     };

3 个答案:

答案 0 :(得分:0)

尝试runOnUiThread:

@Override 
public void onConfigurationChanged(Configuration newConfig){ 
    super.onConfigurationChanged(newConfig); 

    Handler handler = new Handler() {
      public void handleMessage(Message msg) {
         runOnUiThread(new Runnable() {
             @Override
             public void run() {
                 // get the bundle and extract data by key
                 Bundle b = msg.getData();
                 String key = b.getString("My Key");
                 txtName.setText( "Item " + key);
                 txtName.setVisibility(View.VISIBLE);
                 Log.e("TEST MESSAGE",  txtName.getText().toString());
             }
         });
      }
   };
} 

答案 1 :(得分:0)

将OnClickListener创建为固定类,然后将其应用为:

MyListener myListener = new MyListener();

btnClose.setOnClickListener(myListener);

通过这种方式,txtName变量不会被设置为最终

答案 2 :(得分:0)

您的UI只能由UI线程更新。尝试这样的方法让UI线程尽快更新界面。这样我觉得你可以避免使用处理程序。

btnClose.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
              //Closing SecondScreen Activity
              Thread background = new Thread(new Runnable() {
                   @Override
                   public void run() {
                    for (int i = 0; i < 30; i++) {
                     try {
                      Thread.sleep(1000);
                      final String val = "My Value: " + String.valueOf(i);
                      // ask UI thread to make the changes as soon as possible
                      txtName.post(new Runnable(){
                     public void run(){
                              txtName.setText( "Item " + val);
                              txtName.setVisibility(View.VISIBLE);
                         });
                      } catch (Exception e) {
                      Log.d("Error", e.toString());
                     }
                    }
                   }
              });
              background.start();
        }
    });