2014-08-28 211 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