android - 引起:android.view.ViewRootImpl $ CalledFromWrongThreadException

时间:2014-05-02 11:56:01

标签: android web-services textview

我试图从Web服务获取用户名和用户ID,我尝试更改其值取决于我从服务器获取的值,当我尝试更改onCreate函数外的textview值时出现此错误:< / p>

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Profile.java

public class Profile extends Activity {

    TextView mymoUserName;
    TextView mymoID;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);      

        setContentView(R.layout.profile);

        getActionBar().hide();

        mymoUserName = (TextView)findViewById(R.id.profMymoUserName);
        mymoID = (TextView)findViewById(R.id.profMymoID);

        getUserInfo();

    }

    protected void getUserInfo() 
    {
        Thread t = new Thread() {

            public void run() {
                Looper.prepare();
                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
                HttpResponse response;

                String URL = "MY_URL";

                try {
                        HttpPost post = new HttpPost(URL);

                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
                        nameValuePairs.add(new BasicNameValuePair("section", "API" ));

                        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                        response = client.execute(post);

                        if(response!=null)
                        {
                            String res = EntityUtils.toString(response.getEntity());
                            JSONObject result = new JSONObject(res);

                            Toast.makeText(getBaseContext(), res , Toast.LENGTH_LONG).show();

                            String username = result.getString("username");
                            mymoUserName.setText(username);

                            String mymoId = result.getString("mymo_id");
                            mymoID.setText(mymoId);

                        }

                } catch(Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
                }

                Looper.loop();
            }
        };

        t.start();      
    }

    @Override
    public void onBackPressed() 
    {
        finish();
    }

2 个答案:

答案 0 :(得分:0)

您正在从线程更新ui。你应该只从ui线程更新ui

Toast.makeText(getBaseContext(), res , Toast.LENGTH_LONG).show();
mymoUserName.setText(username);
mymoID.setText(mymoId);

必须是ui线程。

您可以使用runOnUiThread

runOnUiThread(new Runnable() {
@Override
public void run() {
         String res = EntityUtils.toString(response.getEntity());
         JSONObject result = new JSONObject(res);
         Toast.makeText(getBaseContext(), res , Toast.LENGTH_LONG).show();

         String username = result.getString("username");
         mymoUserName.setText(username);

         String mymoId = result.getString("mymo_id");
         mymoID.setText(mymoId);

}
}); 

但你应该考虑使用AsyncTask。您可以在doInBackground更新ui onPreExecuteonPostExecuteonProgressUpdate

进行后台操作

答案 1 :(得分:0)

您只能通过Android UI线程访问UI。 在runOnUiThread中添加UI访问语句,如下所示..

   runOnUiThread(new Runnable() {

        @Override
        public void run() {
            Toast.makeText(getBaseContext(), res , Toast.LENGTH_LONG).show();

            String username = result.getString("username");
            mymoUserName.setText(username);

            String mymoId = result.getString("mymo_id");
            mymoID.setText(mymoId);
        }
    });