Search code examples
javaandroidhandlerhttphandlersettext

android.view.View.requestLayout(View.java:16431)


We're working in an android app with the 4 version and we need a mysql connection. We are doing this with a php file which returns the data base result. The following code are working because if we debug the project we can see the correct database result in the the System.out.println. line. But, if we try to change one text in our app for the result of the database our app can’t run correctly and the error was in the t.setText(txt); line.

Can anybody help me with this problem? Do you have another idea to do the database connection or is it the correct way?

Thanks a lot.

My code:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //FUNCIONA NO BORRAR
    final httpHandler handler = new httpHandler();
    Thread thread = new Thread()
    {

        @Override
        public void run() {
            try {
                if(true) {
                    sleep(1000);
                    String txt = handler.post("http://www.golftipp.com/pruebas_android/app.php");
                    TextView t = (TextView) findViewById(R.id.textView2);
                    t.setText(txt);
                    System.out.println(txt);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    };
    thread.start();

}

The error

05-30 02:42:50.331    1276-1291/es.softline.connexiodb.app E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-87
Process: es.softline.connexiodb.app, PID: 1276
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
        at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:824)
        at android.view.View.requestLayout(View.java:16431)
        at android.view.View.requestLayout(View.java:16431)
        at android.view.View.requestLayout(View.java:16431)
        at android.view.View.requestLayout(View.java:16431)
        at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
        at android.view.View.requestLayout(View.java:16431)
        at android.widget.TextView.checkForRelayout(TextView.java:6600)
        at android.widget.TextView.setText(TextView.java:3813)
        at android.widget.TextView.setText(TextView.java:3671)
        at android.widget.TextView.setText(TextView.java:3646)
        at es.softline.connexiodb.app.MainActivity$1.run(MainActivity.java:33)

Solution

  • Non-UI threads cannot access views, use runOnUiThread():

    Thread thread = new Thread()
    {
    
        @Override
        public void run() {
            try {
                if(true) {
                    sleep(1000);
                    String txt = handler.post("http://www.golftipp.com/pruebas_android/app.php");
                    TextView t = (TextView) findViewById(R.id.textView2);
    
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // do UI updates here
                            t.setText(txt);
                        }
                    });
    
                    System.out.println(txt);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    };
    thread.start();