简体   繁体   English

全局变量在内部类中不是null,而在外部类中不是null

[英]The global variable is not null inside an inner class but null outside

I Have a global variable named "myEmail" and I want to change it's value inside a method when doing so the variable's value does change but only inside the method (I've checked this using a log) but when trying to use the Variable again after changing the value it's null. 我有一个名为“ myEmail”的全局变量,我想在方法中更改其值,这样变量的值确实会更改,但仅在方法内部(我已使用日志检查过此值)但尝试再次使用变量时更改值后,它为null。

I have no idea what are the causes of this issue. 我不知道导致此问题的原因是什么。 I've tried calling the method on the onCreate since I thought maybe the variable get's called before changing the value but the issue remained the same. 我一直尝试在onCreate上调用该方法,因为我认为也许在更改值之前调用了变量get,但是问题仍然相同。

Here's my code 这是我的代码

public class messagesFragment extends Fragment {
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private String uid = FirebaseAuth.getInstance().getUid();
    private CollectionReference messageRef = db.collection("messages");
    private View v;
    private RecyclerView recyclerView;
    private messageAdapter mAdapter;
    private String myEmail;
    private FloatingActionButton addmsg;

    public messagesFragment() {
    }
    // Creating the fragment view.
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.messages_fragment, container, false);
        addmsg = (FloatingActionButton) v.findViewById(R.id.sendmsgfab);
        addmsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), createmessageActivity.class);
                startActivity(intent);
            }
        });

        getemail();       // Here the variable myEmail is null.
        setupRecycler();

        return v;
    }

    private void getemail(){
        DocumentReference documentReference = db.collection("users").document(uid);
        documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(final DocumentSnapshot documentSnapshot) {
                myEmail = documentSnapshot.get("email").toString();
              // Here the variable myEmail is not null.
            }
        });
        }

    private void setupRecycler() {
        Query query = messageRef.whereEqualTo("receiver", myEmail);
        FirestoreRecyclerOptions<message> options = new FirestoreRecyclerOptions.Builder<message>()
                .setQuery(query, message.class)
                .build();
        mAdapter = new messageAdapter(options);
        recyclerView = (RecyclerView) v.findViewById(R.id.messagesRecycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onStart() {
        super.onStart();
        mAdapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        mAdapter.stopListening();
    }
}

The reason is myEmail is assigned inside a callback method which could be triggered after setupRecycler() is called. 原因是myEmail是在回调方法中分配的,该方法可以在setupRecycler()之后触发。 So you need to do the checking inside setupRecycler() in onSuccess 所以您需要在onSuccess setupRecycler()内部进行检查

public class messagesFragment extends Fragment {
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private String uid = FirebaseAuth.getInstance().getUid();
    private CollectionReference messageRef = db.collection("messages");
    private View v;
    private RecyclerView recyclerView;
    private messageAdapter mAdapter;
    private String myEmail;
    private FloatingActionButton addmsg;

    public messagesFragment() {
    }
    // Creating the fragment view.
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.messages_fragment, container, false);
        addmsg = (FloatingActionButton) v.findViewById(R.id.sendmsgfab);
        addmsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), createmessageActivity.class);
                startActivity(intent);
            }
        });

        getemail();       // Here the variable myEmail is null.

        return v;
    }

    private void getemail(){
        DocumentReference documentReference = db.collection("users").document(uid);
        documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(final DocumentSnapshot documentSnapshot) {
               myEmail = documentSnapshot.get("email").toString();
               runOnUiThread(new Runnable() {
                   public void run() {
                      setupRecycler();
                   }
               });
             }
        });
     }

     private void setupRecycler() {      
          Query query = messageRef.whereEqualTo("receiver", myEmail);
          FirestoreRecyclerOptions<message> options = new FirestoreRecyclerOptions.Builder<message>()
            .setQuery(query, message.class)
            .build();
          mAdapter = new messageAdapter(options);
          recyclerView = (RecyclerView) v.findViewById(R.id.messagesRecycler);
          recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
          recyclerView.setAdapter(mAdapter);
          mAdapter.startListening();
     }

     @Override
     public void onStart() {
         super.onStart();
     }

     @Override
     public void onStop() {
         super.onStop();
        mAdapter.stopListening();
     }
}

Returning value of your getemail method is void! 您的getemail方法的返回值无效! please replace String with void on your method. 请在您的方法上将String替换为void。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM