【问题标题】:Unreachable Statement in fragment activity片段活动中的不可达语句
【发布时间】:2020-12-07 17:59:52
【问题描述】:

什么是unreachable语句。 我无法使用firebasefirestore 并且method片段视图 中是无法访问的语句。 怎么用方法等东西,什么是unreachable语句?

    private FragmentPostfragBinding binding;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding=FragmentPostfragBinding.inflate(inflater,container,false);
        View view=binding.getRoot();
        return view;


        userview();


    }

    private void userview() {
        FirebaseFirestore db;

        db=FirebaseFirestore.getInstance();
        DocumentReference ref = db.collection("user").document(FirebaseAuth.getInstance().getCurrentUser().getUid());
        ref.get()
                .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>(){
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                        if(documentSnapshot.exists()){

                            String name=documentSnapshot.getString("username");
                            binding.user.setText(name);

                        }
                        else{
                            Toast.makeText(getActivity(), "Sorry, your information is not saved!", Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(getActivity(),otherinfo.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                        }
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(getActivity(), "Error!", Toast.LENGTH_SHORT).show();
                        Log.i(TAG, "onFailure: "+e.toString());
                    }
                });
    }
}

【问题讨论】:

    标签: java android firebase android-fragments android-fragmentactivity


    【解决方案1】:

    “无法访问的语句”意味着您有一些代码在任何情况下都不会运行。在您的情况下,它指的是对 userview() 的调用,它永远不会运行,因为它遵循 return 语句。返回将阻止 userview() 被调用。

    你需要弄清楚你真正想要在这里发生什么,并正确编码。

    【讨论】:

    • 你能描述更多吗,我如何调用我只想访问数据库并在片段视图中获取信息的方法。
    • 至少,您必须在从函数返回之前执行此操作。
    猜你喜欢
    • 2013-10-18
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 2021-06-09
    • 2013-06-15
    相关资源
    最近更新 更多