Android中的错误“只有创建视图层次结构的原始线程才能触摸其视图”[重复]

Posted

技术标签:

【中文标题】Android中的错误“只有创建视图层次结构的原始线程才能触摸其视图”[重复]【英文标题】:Error "Only the original thread that created a view hierarchy can touch its views" in Android [duplicate] 【发布时间】:2013-03-27 00:04:30 【问题描述】:

我正在创建一个使用 androidphp 登录的简单应用程序,但出现错误,谁能帮助我?我正在尝试在作为客户端的 Android 和作为服务器端的 PHP/mysql 之间建立关系,但仍然没有成功获得响应。

AndroidLogin.java

package com.sencide;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidLogin extends Activity implements OnClickListener 


     Button ok,back,exit;
     TextView result;


    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

     // Login button clicked
        ok = (Button)findViewById(R.id.btn_login);
        ok.setOnClickListener(this);

        result = (TextView)findViewById(R.id.lbl_result);



    



    public void postLoginData() 
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
         Log.e("Responce-->","after httpclient");
        /* login.php returns true if username and password is equal to saranga */
        HttpPost httppost = new HttpPost("http://10.0.2.2/login.php");
        Log.e("Responce-->","after httppost");
        try 
            // Add user name and password
         EditText uname = (EditText)findViewById(R.id.txt_username);
         String username = uname.getText().toString();

         EditText pword = (EditText)findViewById(R.id.txt_password);
         String password = pword.getText().toString();

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", username));
            nameValuePairs.add(new BasicNameValuePair("password", password));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            Log.e("Responce-->","after using the list name pair");

            // Execute HTTP Post Request
            Log.w("SENCIDE", "Execute HTTP Post Request");
            HttpResponse response = httpclient.execute(httppost);
            Log.e("Responce-->","after execute the http response");
          //  String str = inputStreamToString(response.getEntity().getContent()).toString();
            String str = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            //Log.w("SENCIDE", str);

            Log.e("Responce-->",""+str);

            if(str.toString().equalsIgnoreCase("true"))
            
             Log.w("SENCIDE", "TRUE");
             result.setText("Login successful");  
            else
            
             Log.w("SENCIDE", "FALSE");
             result.setText(str);            
            

         catch (ClientProtocolException e) 
         e.printStackTrace();
         catch (IOException e) 
         e.printStackTrace();
        
     



    private StringBuilder inputStreamToString(InputStream is) 
        String line = "";
        StringBuilder total = new StringBuilder();
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try 
         while ((line = rd.readLine()) != null) 
           total.append(line);
         
         catch (IOException e) 
         e.printStackTrace();
        
        // Return full string
        return total;
       

        /* login.php returns true if username and password is equal to test1 */


    @Override
    public void onClick(View view) 
        // TODO Auto-generated method stub
        final String resultText;
        if(str.toString().equalsIgnoreCase("true")) 
            resultText = "Login successful";
        else 
            resultText = str;
        

        if(view == ok)
             Thread t = new Thread()


                    public void run()
                        postLoginData();

                        result.post(new Runnable() 
                            public void run() 
                                result.setText(resultText);
                            
                        );
                    
                ;
                t.start();

          
    






日志猫

   04-04 19:04:20.504: E/Responce-->(457): after httpclient
04-04 19:04:20.517: E/Responce-->(457): after httppost
04-04 19:04:20.544: E/Responce-->(457): after using the list name pair
04-04 19:04:20.544: W/SENCIDE(457): Execute HTTP Post Request
04-04 19:04:20.824: E/Responce-->(457): after execute the http response
04-04 19:04:20.834: E/Responce-->(457): true
04-04 19:04:20.834: W/SENCIDE(457): TRUE
04-04 19:04:20.844: W/dalvikvm(457): threadid=9: thread exiting with uncaught exception (group=0x40015560)
04-04 19:04:20.912: E/AndroidRuntime(457): FATAL EXCEPTION: Thread-10
04-04 19:04:20.912: E/AndroidRuntime(457): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
04-04 19:04:20.912: E/AndroidRuntime(457):  at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
04-04 19:04:20.912: E/AndroidRuntime(457):  at android.view.ViewRoot.requestLayout(ViewRoot.java:629)
04-04 19:04:20.912: E/AndroidRuntime(457):  at android.view.View.requestLayout(View.java:8267)
04-04 19:04:20.912: E/AndroidRuntime(457):  at android.view.View.requestLayout(View.java:8267)
04-04 19:04:20.912: E/AndroidRuntime(457):  at android.view.View.requestLayout(View.java:8267)
04-04 19:04:20.912: E/AndroidRuntime(457):  at android.view.View.requestLayout(View.java:8267)
04-04 19:04:20.912: E/AndroidRuntime(457):  at android.view.View.requestLayout(View.java:8267)
04-04 19:04:20.912: E/AndroidRuntime(457):  at android.widget.TextView.checkForRelayout(TextView.java:5521)
04-04 19:04:20.912: E/AndroidRuntime(457):  at android.widget.TextView.setText(TextView.java:2724)
04-04 19:04:20.912: E/AndroidRuntime(457):  at android.widget.TextView.setText(TextView.java:2592)
04-04 19:04:20.912: E/AndroidRuntime(457):  at android.widget.TextView.setText(TextView.java:2567)
04-04 19:04:20.912: E/AndroidRuntime(457):  at com.sencide.AndroidLogin$1.run(AndroidLogin.java:136)
04-04 19:04:23.464: I/Process(457): Sending signal. PID: 457 SIG: 9

php代码

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="testlogin"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['username'];
$mypassword=$_POST['password'];

// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
echo "true";

else 
echo "Login Failed";

?>

【问题讨论】:

我自己不是 Android 程序员,但该错误消息(现在在问题的标题中)非常清楚。好像您正在访问另一个线程私有的东西。你有searched for it吗? - 那里有很多结果。 您不能在 UI 线程之外修改 UI 小部件。只有 UI 线程可以执行此操作并运行 result.setText(str);在一个新线程上。 runOnUIThread() 可能会对您有所帮助,但我也会从 onClick() 方法中删除 public void postLoginData() 方法主体,它看起来是在 onClick() 内部创建的,这很奇怪。 @SergeyBenner 我编辑了 androidlogin.java 看看,但问题是我得到字符串 str 无法解析,因为 str 是在 postlogindata () 方法中初始化的 我更改了您的代码,看看是否适合您。也添加您的package。不要忘记它。 【参考方案1】:

解决方案在 logcat 的日志中:

04-04 18:38:47.596: E/AndroidRuntime(413): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

发布用户/密码有效。您可以得到“真实”响应,但随后您尝试在线程外部创建的视图中设置标签或类似内容。这是不行的。

result.setText("Login successful");

查看有关线程/进程的 Android SDK 文档,了解如何从其他线程更新视图:http://developer.android.com/guide/components/processes-and-threads.html

【讨论】:

所以你的意思是我必须在线程中显示响应?? 我编辑了我的问题,我确实在线程中显示了结果,但我仍然遇到同样的问题【参考方案2】:

好的,我对您的代码进行了一些修改 它仍然很丑,需要大量重构 但告诉我它是否会引发任何异常。

编辑: 添加了您的 usernamepassword 传递给请求 作为postLoginData(String password,String username) 方法的参数。 应该这样做。


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidLogin extends Activity implements OnClickListener 


    Button ok, back, exit;
    TextView result;
    EditText uname;
    EditText pword;

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        StrictMode.ThreadPolicy policy = 
        new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        // Login button clicked
        ok = (Button) findViewById(R.id.btn_login);
        ok.setOnClickListener(this);
        uname = (EditText) findViewById(R.id.txt_username);
        pword = (EditText) findViewById(R.id.txt_password);
        result = (TextView) findViewById(R.id.lbl_result);


    

    @Override
    public void onClick(View view) 
        // TODO Auto-generated method stub
        final String resultText;   

        final String username = uname.getText().toString();           
        final String password = pword.getText().toString();


        if (view == ok) 
            Thread t = new Thread() 

                public void run() 
                    postLoginData(username,password);
                
            ;
            t.start();

        
    

    private void postLoginData(String username,String password) 

        try 
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            Log.e("Response-->", "after httpclient");
        /* login.php returns true if username and password is equal to saranga */
            HttpPost httppost = new HttpPost("http://10.0.2.2/login.php");
            Log.e("Response-->", "after httppost");


            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", username));
            nameValuePairs.add(new BasicNameValuePair("password", password));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            Log.e("Responce-->", "after using the list name pair");

            // Execute HTTP Post Request
            Log.w("SENCIDE", "Execute HTTP Post Request");
            HttpResponse response = httpclient.execute(httppost);
            Log.e("Responce-->", "after execute the http response");
            String str = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);

            if (str.toString().equalsIgnoreCase("true")) 

                runOnUiThread(new Runnable() 
                    public void run() 
                        result.setText("Login Successful");

                    
                );


             else 
                runOnUiThread(new Runnable() 
                    public void run() 
                        result.setText("Duh");

                    
                );
            

         catch (ClientProtocolException e) 
            e.printStackTrace();
         catch (IOException e) 
            e.printStackTrace();
        

    



【讨论】:

这段代码以某种方式工作,但它只是显示这些消息 04-04 22:02:40.218: E/Response-->(578): after httpclient 04-04 22:02:40.224 : E/Response-->(578): 在 httppost 04-04 22:02:40.254: E/Responce-->(578): 在使用列表名称对之后 04-04 22:02:40.254: W/SENCIDE (578): Execute HTTP Post Request 04-04 22:02:40.484: E/Responce-->(578): 在执行 http 响应后 没有登录成功或 Duh 不知何故... :))) 你必须在EntityUtils.toString 行之后打印出你的str 例如Log.d("str",str); 变量,看看里面有什么。您在回复中收到的内容,然后据此构成您的条件。 它打印正确,但我的意思是在应用程序中它必须显示登录成功为什么它没有你能帮助我我知道我在打扰你谢谢 好的,现在可以了,非常好,谢谢【参考方案3】:

您不能在主 UI 线程之外对 android 视图进行更改。在您的示例中,您正在启动一个新线程并在其上运行 postLoginData() 。因为 postLoginData 还设置了 textview 的文本,所以会出现此错误。

您应该:在新线程上执行 http 请求,然后在 UI 线程上更改文本。

有多种方法可以做到这一点。

AsyncTask Handler

而且这种超级简单但可能不建议使用的方法只是让你站起来。每当您在新线程中对 UI 进行更改时,您都希望这样做,因此在此示例中,当您设置结果文本时。

result.post(new Runnable() 
        public void run() 
            result.setText(str);
        
    );

它将在 UI 线程上运行。你真的应该看看我首先发布的两个选项,否则你会遇到更多麻烦。

** 编辑 ** 您只需替换此代码即可在主线程而不是新线程上发布。

if(str.toString().equalsIgnoreCase("true"))

 Log.w("SENCIDE", "TRUE");
 result.setText("Login successful");  
else

 Log.w("SENCIDE", "FALSE");
 result.setText(str);            

让它看起来像这样:

final String resultText;
if(str.toString().equalsIgnoreCase("true")) 
    resultText = "Login successful";
else 
    resultText = str;

result.post(new Runnable() 
        public void run() 
            result.setText(resultText);
        
    );

【讨论】:

我编辑我的问题,查看 AndroidLogin.java 我将 postlogindata() 放入线程中,但仍然得到相同的错误 我完全不是这个意思哈!恢复到你所拥有的并检查我的编辑。 我编辑了我的问题,并在新线程中显示了 postlogindata(),但现在出现语法错误,您可以看看

以上是关于Android中的错误“只有创建视图层次结构的原始线程才能触摸其视图”[重复]的主要内容,如果未能解决你的问题,请参考以下文章

Android错误:只有创建视图层次结构的原始线程才能触摸其视图[重复]

Android 错误“只有创建视图层次结构的原始线程才能触摸其视图”

错误:只有创建视图层次结构的原始线程才能接触其视图

CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能接触视图

致命例外:JavaBridge - 只有创建视图层次结构的原始线程才能接触其视图

android.view.ViewRootImpl $CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图[重复]