2014-08-28 137 views
-2

我是新的Android应用程序开发时,我按登录按钮我的应用程序崩溃,并给我一个致命异常请帮助我。致命异常:AsyncTask#1 2

同时,我想使用的softkeyboard而不是登录按钮

登录类

public class Login extends Activity implements OnClickListener { 

private ProgressDialog pDialog; 

Button btnLogin; 
    Button btnLinkToRegister; 
    EditText inputEmail; 
    EditText inputPassword; 
    TextView loginErrorMsg; 

    // JSON Response node names 


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

     if (android.os.Build.VERSION.SDK_INT > 9) { 
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
     } 

     // Importing all assets like buttons, text fields 
     inputEmail = (EditText) findViewById(R.id.loginEmail); 
     inputPassword = (EditText) findViewById(R.id.loginPassword); 
     btnLogin = (Button) findViewById(R.id.btnLogin); 

     btnLogin.setOnClickListener(this); 

     loginErrorMsg = (TextView) findViewById(R.id.login_error); 

     // Login button Click Event 
    } 


    public void onClick(View v) 
    { 


     if(v.getId()==R.id.btnLogin) 
     { 
      new LoginExecute().execute(); 

     } 
    } 
    class LoginExecute extends AsyncTask<String, String, String> 
    { 
     String email = inputEmail.getText().toString(); 
     String password = inputPassword.getText().toString(); 
     UserFunctions userFunction = new UserFunctions(); 
     JSONObject json = userFunction.loginUser(email, password); 


     protected String doInBackground(String... args) 
     { 
      try { 
       if(email.trim().length()>0 || password.trim().length()>0) 
       { 

        if (json.getString("STATUS") != null) { 
         loginErrorMsg.setText(""); 
         String res3 = json.getString("STATUS"); 
         System.out.println("Status = " +res3); 
         if(Integer.parseInt(res3) == 1){ 

         if (json.getString("role").equalsIgnoreCase("Inspector")) 
         { 
          loginErrorMsg.setText(""); 
          String res1 = json.getString("role"); 
          System.out.println("Role =" +res1); 
         String area = json.getString("area"); 
         String street = json.getString("street"); 
         String user_id = json.getString("user_id"); 
          if(res1.equals("Inspector")) 
          { 
           Intent dashboard = new Intent(getApplicationContext(), Inspector.class); 

           // Close all views before launching Dashboard 
           dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
           startActivity(dashboard); 
          // finish(); 

          } 

         } 
         else if (json.getString("role").equalsIgnoreCase("Clamper")) 
         { 
          loginErrorMsg.setText(""); 
          String res1 = json.getString("role"); 
          ; 
          if(res1.equals("Clamper")) 
          { 
           Intent dashboard1 = new Intent(getApplicationContext(), Clamper.class); 

           // Close all views before launching Dashboard 
           dashboard1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
           startActivity(dashboard1); 
           //finish(); 

          } 

         } 
         else if (json.getString("role").equalsIgnoreCase("Declamper")) 
         { 
          loginErrorMsg.setText(""); 
          String res22 = json.getString("role"); 

          System.out.println("Role =" +res22); 
          if(res22.equals("Declamper")) 
          { 
           Intent dashboard2 = new Intent(getApplicationContext(), Declamper.class); 

           // Close all views before launching Dashboard 
           dashboard2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
           startActivity(dashboard2); 
           //finish(); 

          } 

         } 
         else if (json.getString("role").equalsIgnoreCase("Tower")) 
         { 
          loginErrorMsg.setText(""); 
          String res8 = json.getString("role"); 

          System.out.println("Role =" +res3); 
          if(res8.equals("Tower")) 
          { 
           Intent dashboard3 = new Intent(getApplicationContext(), Tower.class); 

           // Close all views before launching Dashboard 
           dashboard3.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
           startActivity(dashboard3); 
           //finish(); 

          } 

         } 

        }else{ 
         // Error in login 
         loginErrorMsg.setText("Incorrect username/password"); 
        } 
       } 
       }else{ 
        loginErrorMsg.setText("Blank username/password"); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
     protected void onPostExecute(String file_url) { 
      pDialog = new ProgressDialog(Login.this); 
      pDialog.setMax(25); 
      pDialog.setMessage("Processing...."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
      long delayInMillis = 2000; 
      Timer timer = new Timer(); 
      timer.schedule(new TimerTask() { 
       @Override 
       public void run() { 
        pDialog.dismiss(); 
       } 
      }, delayInMillis); 
     } 
    } 

日志错误文件

08-28 09:08:13.136: W/dalvikvm(1348): threadid=11: thread exiting with uncaught exception (group=0xb2b0cba8) 
08-28 09:08:13.276: D/dalvikvm(1348): GC_FOR_ALLOC freed 205K, 7% free 3875K/4156K, paused 35ms, total 35ms 
08-28 09:08:13.286: E/AndroidRuntime(1348): FATAL EXCEPTION: AsyncTask #1 
08-28 09:08:13.286: E/AndroidRuntime(1348): Process: com.example.testlogin, PID: 1348 
08-28 09:08:13.286: E/AndroidRuntime(1348): java.lang.RuntimeException: An error occured while executing doInBackground() 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at android.os.AsyncTask$3.done(AsyncTask.java:300) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at java.lang.Thread.run(Thread.java:841) 
08-28 09:08:13.286: E/AndroidRuntime(1348): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:857) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at android.view.ViewGroup.invalidateChild(ViewGroup.java:4320) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at android.view.View.invalidate(View.java:10935) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at android.view.View.invalidate(View.java:10890) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at android.widget.TextView.checkForRelayout(TextView.java:6587) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at android.widget.TextView.setText(TextView.java:3813) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at android.widget.TextView.setText(TextView.java:3671) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at android.widget.TextView.setText(TextView.java:3646) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at com.example.testlogin.Login$LoginExecute.doInBackground(Login.java:185) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at com.example.testlogin.Login$LoginExecute.doInBackground(Login.java:1) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at android.os.AsyncTask$2.call(AsyncTask.java:288) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
08-28 09:08:13.286: E/AndroidRuntime(1348):  ... 4 more 
+0

然后最新的解决方案 – 2014-08-28 13:27:05

+0

你甚至读过答案? – Apoorv 2014-08-28 13:27:46

+0

或这一个:http://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-c​​reated-a-view-hierarchy-can-touch-its-vi – pomber 2014-08-28 13:28:05

回答

0

的问题是回车键与这些线(和类似的):

loginErrorMsg.setText(""); 

AsyncTask被设计为在另一个线程中运行,并且您不能从另一个线程修改UI(如果允许,这可能非常糟糕)。

您需要创建一个Runnable,然后在UI线程上运行它,或像执行其他UI元素那样在onPostExecute中执行该代码。

像这样:

String errorText = ""; 

private Runnable updateErrorMsg = new Runnable() { 

    @Override 
    public void run() { 
     loginErrorMsg.setText(errorText); 
    } 

}; 


     protected String doInBackground(String... args) 
    { 
     try { 
      if(email.trim().length()>0 || password.trim().length()>0) 
      { 

       if (json.getString("STATUS") != null) { 
        loginErrorMsg.setText(""); 
        String res3 = json.getString("STATUS"); 
        System.out.println("Status = " +res3); 
        if(Integer.parseInt(res3) == 1){ 

        if (json.getString("role").equalsIgnoreCase("Inspector")) 
        { 
         // change to this 
         errorText = ""; 
         runOnUiThread(updateErrorMsg); 
         String res1 = json.getString("role"); 
         System.out.println("Role =" +res1); 
        String area = json.getString("area"); 
        String street = json.getString("street"); 
        String user_id = json.getString("user_id"); 
         if(res1.equals("Inspector")) 
         { 
          Intent dashboard = new Intent(getApplicationContext(), Inspector.class); 

          // Close all views before launching Dashboard 
          dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
          startActivity(dashboard); 
         // finish(); 

         } 

        } 
        else if (json.getString("role").equalsIgnoreCase("Clamper")) 
        { 
         // change to this 
         errorText = ""; 
         runOnUiThread(updateErrorMsg); 
         String res1 = json.getString("role"); 
         ; 
         if(res1.equals("Clamper")) 
         { 
          Intent dashboard1 = new Intent(getApplicationContext(), Clamper.class); 

          // Close all views before launching Dashboard 
          dashboard1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
          startActivity(dashboard1); 
          //finish(); 

         } 

        } 
        else if (json.getString("role").equalsIgnoreCase("Declamper")) 
        { 
         loginErrorMsg.setText(""); 
         String res22 = json.getString("role"); 

         System.out.println("Role =" +res22); 
         if(res22.equals("Declamper")) 
         { 
          Intent dashboard2 = new Intent(getApplicationContext(), Declamper.class); 

          // Close all views before launching Dashboard 
          dashboard2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
          startActivity(dashboard2); 
          //finish(); 

         } 

        } 
        else if (json.getString("role").equalsIgnoreCase("Tower")) 
        { 
         // change to this 
         errorText = ""; 
         runOnUiThread(updateErrorMsg); 
         String res8 = json.getString("role"); 

         System.out.println("Role =" +res3); 
         if(res8.equals("Tower")) 
         { 
          Intent dashboard3 = new Intent(getApplicationContext(), Tower.class); 

          // Close all views before launching Dashboard 
          dashboard3.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
          startActivity(dashboard3); 
          //finish(); 

         } 

        } 

       }else{ 
        // Error in login 
        // loginErrorMsg.setText("Incorrect username/password"); 
        // change to this 
        errorText = "Incorrect username/password"; 
        runOnUiThread(updateErrorMsg); 
       } 
      } 
      }else{ 
       // loginErrorMsg.setText("Blank username/password"); 
       // change to this 
       errorText = "Blank username/password"; 
       runOnUiThread(updateErrorMsg); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
+0

可以请你提供我的源代码 – 2014-08-28 13:30:08

+0

我做了编辑 - 还有其他方法,这只是一个例子。我可能会设置错误文本,因为我已经在这里显示,然后把错误消息更新在onPostExecute,但我认为这是一个更有用的例子 – Jim 2014-08-28 15:22:54

+0

是它的工作非常感谢,请你解释我怎么样错误消息更新在onPostExecute也请我想学习。 – 2014-08-30 06:19:00

0

你不能在TextViewdoInBackgroundsetText,你应该更新里面onPostExecute您的UI一旦任务执行完毕,或使用一个线程任务执行过程中更改UI (不建议)。

如果需要显示进度,可以使用onProgressUpdatepublishProgress

+0

你能否给我提供源代码 – 2014-08-28 13:30:57

+0

你可能需要重做你的整个任务,以解析doInBackground中的整个JSON,然后*使用导致数据更新您的用户界面,启动新的活动等... – 2Dee 2014-08-28 13:50:58