在doInbackground中调用局部变量

时间:2016-06-17 09:02:46

标签: android android-asynctask

我使用asynctask但是我无法在doInBackground方法中处理局部变量所以在我的情况下当我想设置局部变量test我得到了一个错误 那我该怎么办? 当我用这种方式设置测试时,测试=" testme"没有这个我没有得到错误,但测试始终为空。 那么我该怎么做才能在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())   ;

    }
}

1 个答案:

答案 0 :(得分: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;
    }