为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

繁花如伊 2023-08-16 17:50:28
单击 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
查看完整描述

1 回答

?
智慧大石

TA贡献1946条经验 获得超3个赞

您无法使用 startActivity 打开片段,您需要实现一个回调,该回调会在您的用户个人资料成功更新时触发。在您的主机活动中创建一个界面,如下所示:


public interface UpdateProfileSuccess{

    void presentAccountFragment();

}

还在活动中将其实例化为:


UpdateProfileSuccess callback = new UpdateProfileSuccess(){

@Override

void 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。


查看完整回答
反对 回复 2023-08-16
  • 1 回答
  • 0 关注
  • 75 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信