2014-01-19 23 views
0

我正在使用的应用程序有一个连接到mysql数据库的登录/注册表,起初我在UI Thread上运行了所有东西,后来我发现它不能正常工作,因为它反对在Android的UI Thread上运行长代码。我试图编辑我的代码,在我添加的新Thread上运行长任务。 。现在,我的应用程序注册我看到的结果在MySQL,但我的应用程序不断,因为这个错误如何运行runOnUithread?

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

错误是可以理解的,但我不知道该怎么办好了ViewView发回给UI Thread的关闭。

我做了一些研究有关runOnuithread,但我不知道在哪里把它放在我的代码,或天气我把新Thread我在错误的地方之前添加到开始请

谁能帮助我修复这个

这里是代码

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.register); 

    // Importing all assets like buttons, text fields 
    inputFullName = (EditText) findViewById(R.id.registerName); 
    inputEmail = (EditText) findViewById(R.id.registerEmail); 
    inputPassword = (EditText) findViewById(R.id.registerPassword); 
    btnRegister = (Button) findViewById(R.id.btnRegister); 
    btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen); 
    registerErrorMsg = (TextView) findViewById(R.id.register_error); 

    // Register Button Click event 
    btnRegister.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View view) { 
      /** According with the new StrictGuard policy, running long tasks on the Main UI thread is not possible 
      So creating new thread to create and execute http operations */ 
      new Thread (new Runnable() { 
       @Override 
       public void run() { 
      // 
      String name = inputFullName.getText().toString(); 
      String email = inputEmail.getText().toString(); 
      String password = inputPassword.getText().toString(); 
      UserFunctions userFunction = new UserFunctions(); 

      JSONObject json = userFunction.registerUser(name, email, password); 

      // check for login response 
      try { 
       // 

       // 
       if (json.getString(KEY_SUCCESS) != null) { 

        registerErrorMsg.setText(""); 
        String res = json.getString(KEY_SUCCESS); 
        if(Integer.parseInt(res) == 1){ 
         // user successfully registred 
         // Store user details in SQLite Database 
         DatabaseHandler db = new DatabaseHandler(getApplicationContext()); 
         JSONObject json_user = json.getJSONObject("user"); 

         // Clear all previous data in database 
         userFunction.logoutUser(getApplicationContext()); 
         db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));       
         // Launch Dashboard Screen 
         Intent dashboard = new Intent(getApplicationContext(), MainActivity.class); 
         // Close all views before launching Dashboard 
         dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
         startActivity(dashboard); 
         // Close Registration Screen 
         finish(); 
        }else{ 
         // Error in registration 
         registerErrorMsg.setText("Error occured in registration"); 
        } 
       }// 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
      }).start(); 
     } 
    }); 

    // Link to Login Screen 
    btnLinkToLogin.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View view) { 

      Intent i = new Intent(getApplicationContext(), 
        LoginActivity.class); 
      startActivity(i); 
      // Close Registration View 
      finish(); 
     } 
    }); 
} 
} 

回答

1

的片断因为你似乎有UI代码混合网络代码,我不打算尝试写吧。我可以告诉你应该怎么做,并且假设你可以自己重新安排代码,因为你知道它的全部功能。

好了,里面你run()你把你的网络代码,那么当你需要更新UI你可以有

runOnUiThread(new Runnable() 
{ 
    @Override 
    public void run() 
    { 
     // code to update UI 
    } 
}); 

虽这么说,我建议使用AsyncTask为您的网络运营。这使得它更容易,因为它已经具备了后台工具的功能并更新了UI。您开始执行任务并运行doInBackground(),这就是您执行所有网络操作的位置。然后你可以在AsyncTasks其他3种方法中更新UI

See this answer有关使用AsyncTask的示例以及上述文档的链接。

+0

我很高兴我的答案有帮助,但这不是编辑的目的。如果它有帮助,您可以通过点击旁边的复选标记来接受这个答案作为正确的答案,并且/或者如果有帮助的话可以提高链接的答案(如果您有足够的代表,现在您只能接受此答案)。 – codeMagic

+0

感谢和我的不好,但我终于明白了工作你指出我在正确的方向。 – BMC

+0

没问题,因为你没有问过很多问题,我想我会给你一些关于它通常如何工作的信息。真高兴你做到了 – codeMagic