Question

I'm trying to get username and user ID from web service and I try to change his value depend on value I got it from server, When I try to change textview value outside onCreate function I have this error:

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();
    }
Was it helpful?

Solution

You are updating ui from a thread. You should update ui from ui thread only

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

must be in ui thread.

You can use 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);

}
}); 

But you should consider using AsyncTask. You can do your background operation in doInBackground update ui onPreExecute, onPostExecute and onProgressUpdate

OTHER TIPS

You can access UI through android UI thread only. Add UI accessing statements in runOnUiThread as follows..

   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);
        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top