2016-06-17 49 views
0

我使用asynctask但我無法處理doInBackground方法中的局部變量,所以在我的情況下,當我想設置局部變量測試時出現錯誤 那麼我該怎麼辦? 當我用這種方式設置測試test =「testme」沒有這個我沒有得到一個錯誤,但測試總是爲空。 所以我能做些什麼來設置一個局部變量doinbackground方法調用doInbackground中的局部變量

public class ConnectTask extends AsyncTask{ 
    Context context; 
    User user2 ; 
    String test; 
    private User authentifcatedUser; 

    public ConnectTask(User authentifcatedUser) { 
     this.authentifcatedUser = authentifcatedUser; 

    } 

    public User getAuthentifcatedUser() { 
     return authentifcatedUser; 
    } 

    public void setAuthentifcatedUser(User authentifcatedUser) { 
     this.authentifcatedUser = authentifcatedUser; 
    } 



    public String getTest() { 
     return test; 
    } 

    public User getUser(){ 
     return this.user2; 
    } 

    public void setTest(String test) { 
     this.test = test; 
    } 

    @Override 
    protected Object doInBackground(Object[] params) { 

     connect(); 

     return test; 
    } 

    public String connect(){ 


     final Firebase ref = new Firebase("https://app.firebaseio.com/users"); 
       Query query = ref.orderByChild("username").equalTo("testme"); 
       query.addListenerForSingleValueEvent(new ValueEventListener() { 

        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 
         for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) { 
          User user = userSnapshot.getValue(User.class); 

          System.out.println("my birth day is "+user.getDateOfBirth()); 

          user2=new User(user.getUsername(),user.getEmail(),user.getPassword(),user.getSignUpDate(),user.dateOfBirth); 
          System.out.println("my email is" + user2.getEmail()); 
          test="aaaaaaaaa"; 


         } 

         /* User user = dataSnapshot.getValue(User.class); 
         Log.w("tag", dataSnapshot.toString());*/ 

        } 

        @Override 
        public void onCancelled(FirebaseError firebaseError) { 

        } 

       }); 


     return test; 


    } 

    @Override 
    protected void onPostExecute(Object o) { 
     super.onPostExecute(o); 

     System.out.println("retuuuurn"+connect()) ; 

    } 
} 
+0

你想要什麼?你是否想說'test =「aaaaaaaaa」;'是不會設置'test'的值? –

+0

是的,我想設置測試值 – fatma19

+0

參考@Sohail Zahid的答案在下面..這是正確的 –

回答

2

Asynctask doInBackground()方法工作在後臺線程,你不能操縱後臺線程這就是爲什麼你得到問題的UI線程對象。

要在後臺線程的改變可以通過使用下面的代碼在doInBackground()

runOnUiThread(new Runnable() { 

     @Override 
      public void run() { 
      //btn.setText("#" + i); 
      test ="???"; 
      } 
     }); 

更新你的情況

public String connect() { 

     final Firebase ref = new Firebase("https://app.firebaseio.com/users"); 
     Query query = ref.orderByChild("username").equalTo("testme"); 
     query.addListenerForSingleValueEvent(new ValueEventListener() { 

      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) { 
        User user = userSnapshot.getValue(User.class); 

        System.out.println("my birth day is " + user.getDateOfBirth()); 

        user2 = new User(user.getUsername(), user.getEmail(), user.getPassword(), user.getSignUpDate(), user.dateOfBirth); 
        System.out.println("my email is" + user2.getEmail()); 
//     test = "aaaaaaaaa"; 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          test = "aaaaaaaaa"; 
         } 
        }); 

         /* User user = dataSnapshot.getValue(User.class); 
         Log.w("tag", dataSnapshot.toString());*/ 
       } 
      } 

      @Override 
      public void onCancelled(FirebaseError firebaseError) { 

      } 
     }); 
     return test; 
    } 
+0

runOnUiThread是預定義的嗎? – fatma19

+0

是的,它已經退出。 –

+0

,但當我在doInBackground中調用它時,它不起作用 – fatma19