Android - 如何在签名模式下从另一个线程更改UI

时间:2014-06-14 11:40:33

标签: android

我需要从另一个线程更改UI,因此我使用了以下代码:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        status_txt.setText("Some msg ...");
    }
});

TextView status_txt = (TextView) findViewById(R.id.status);

在调试模式下,当我使用USB调试在手机中测试应用程序时,一切正常,UI正确更改。但是,当我将应用程序导出并签名为APK文件时,安装后UI无法更改。

有什么问题?


更新我的整个代码是:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.TextView;


@SuppressLint("DefaultLocale")
public class Httpd extends Activity
{
    TextView textIpaddr;

    private WebServer server;
    /** Called when the activity is first created. */
    @SuppressLint("DefaultLocale")
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        server = new WebServer();
        try {
            server.start();
        } catch(IOException ioe) {
            Log.w("Httpd", "The server could not start.");
        }
        Log.w("Httpd", "Web server initialized.");
    }


    // DON'T FORGET to stop the server
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (server != null)
            server.stop();
    }

    public void setTextHistory(String input) {
        textIpaddr.setText(input);
    }

    private class WebServer extends NanoHTTPD {

        public WebServer()
        {
            super(8080);
        }

        @Override
        public Response serve(String uri, Method method, 
                Map<String, String> header,
                Map<String, String> parameters,
                Map<String, String> files) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    setTextHistory("Some Text ...");
                }
            });
        }

    }

}

1 个答案:

答案 0 :(得分:1)

只需更新您的ADT边界,错误就会消失。 http://developer.android.com/sdk