0

我无法理解为什么我的后台线程在屏幕方向更改后无法更改 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());
      }

     };
4

3 回答 3

0

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

MyListener myListener = new MyListener();

btnClose.setOnClickListener(myListener);

这样txtName变量就不能设置为final

于 2013-07-28T22:07:33.243 回答
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();
        }
    });
于 2013-07-28T22:08:20.983 回答
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());
             }
         });
      }
   };
} 
于 2013-07-27T12:17:18.703 回答