java.lang.RuntimeException:无法实例化活动

单击 AccountFragment 中的按钮后出现此错误


public class AccountFragment extends Fragment  {

    private static final String TAG = "Profile" ;

    FirebaseAuth firebaseAuth;

    FirebaseUser currentUser;

    DatabaseReference databaseReference;

    TextView tv_name, tv_email, tv_gender, tv_home, tv_phone;

    private String userID;

    Button btn_update;




    public AccountFragment() {

        // Required empty public constructor

    }



    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_account, container, false);


        tv_name=view.findViewById(R.id.tv_name);

        tv_email=view.findViewById(R.id.tv_email);

        tv_gender=view.findViewById(R.id.tv_gender);

        tv_home=view.findViewById(R.id.tv_home);

        tv_phone=view.findViewById(R.id.tv_phone);

        btn_update=view.findViewById(R.id.btn_update);



        //ini

        firebaseAuth = FirebaseAuth.getInstance();

        currentUser = firebaseAuth.getCurrentUser();

        userID = currentUser.getUid();

        databaseReference = FirebaseDatabase.getInstance().getReference("Customer").child(firebaseAuth.getUid());


        //For Retrieve Information

        databaseReference.addValueEventListener(new ValueEventListener() {



            @Override

            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                Log.d( TAG, "onDataChange :" +dataSnapshot);


                    Customer cust = dataSnapshot.getValue(Customer.class);

                    tv_name.setText(cust.getName());

                    tv_email.setText(cust.getEmail());

                    tv_gender.setText(cust.getGender());

                    tv_home.setText(cust.getHome_address());

                    tv_phone.setText(cust.getTelephone_number());


            }

单击按钮后,我收到此错误 2019-09-22 22:20:32


繁花如伊
浏览 99回答 1
1回答

智慧大石

您无法使用 startActivity 打开片段,您需要实现一个回调,该回调会在您的用户个人资料成功更新时触发。在您的主机活动中创建一个界面,如下所示:public interface UpdateProfileSuccess{    void presentAccountFragment();}还在活动中将其实例化为:UpdateProfileSuccess callback = new UpdateProfileSuccess(){@Overridevoid presentAccountFragment(){getSupportFragmentManager().beginTransaction().replace(R.id.container,new         AccuontFragment()).commit();    }}在您的更新配置文件片段中,创建一个构造函数,将该回调作为:private MainActivity.UpdateProfileSuccess callback;public UpdateProfileFragment(MainActivity.UpdateProfileSuccess callback){    this.callback = callback;}最后在请求的 onComplete 中将该回调消耗为:callback.presentAccountFragment();这样您的 Activity 将负责呈现 AccountFragment。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java