Android getFragmentManger()已弃用,注销()错误
我有一些问题,我不知道为什么它会给我错误,这个错误是在更新之后出现的 渐变至最新版本:Android getFragmentManger()已弃用,注销()错误,android,google-cloud-firestore,firebase-authentication,android-fragmentactivity,Android,Google Cloud Firestore,Firebase Authentication,Android Fragmentactivity,我有一些问题,我不知道为什么它会给我错误,这个错误是在更新之后出现的 渐变至最新版本: implementation 'com.google.firebase:firebase-auth:19.3.2' implementation 'com.google.firebase:firebase-database:19.3.1' implementation 'com.google.firebase:firebase-storage:19.1.1' implementation 'com.googl
implementation 'com.google.firebase:firebase-auth:19.3.2'
implementation 'com.google.firebase:firebase-database:19.3.1'
implementation 'com.google.firebase:firebase-storage:19.1.1'
implementation 'com.google.firebase:firebase-firestore:21.5.0'
getFragmentManager()的第一件事是不推荐使用
第二期:
单击“注销”并从用户注销后,将显示以下消息:
活动代码为:
public class profileFragment extends Fragment {
ListView list;
TextView t_name;
FirebaseAuth firebaseAuth;
FirebaseFirestore fstore;
String userId_s;
String[] viewList = {"Request", "address", "About us", "Contact us", "Sign-out"};
public profileFragment() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_profile, container, false);
firebaseAuth = FirebaseAuth.getInstance();
fstore = FirebaseFirestore.getInstance();
userId_s = firebaseAuth.getCurrentUser().getUid();
list = v.findViewById(R.id.list);
t_name = v.findViewById(R.id.name_profile);
//get name of user from fstore
final DocumentReference df = fstore.collection("sales").document(userId_s);
df.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException
error) {
t_name.setText(value.getString("email"));
}
});
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,viewList);
list.setAdapter(arrayAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
request();
return;
case 1:
address();
return;
case 2:
about_us();
return;
case 3:
contact_us();
return;
case 4:
sign_out();
return;
}
}
private void about_us() {
Toast.makeText(getActivity(), "soon About us", Toast.LENGTH_SHORT).show();
}
private void contact_us() {
Toast.makeText(getActivity(), "Soon Contact us", Toast.LENGTH_SHORT).show();
}
private void address() {
Toast.makeText(getActivity(), "Welcome address", Toast.LENGTH_SHORT).show();
}
private void request() {
requestFragment fn = new requestFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fram_container, fn);
ft.commit();
}
private void sign_out() {
FirebaseAuth.getInstance().signOut();
Intent intent = new Intent(getActivity(),login_Activity.class);
startActivity(intent);
}
});
return v;
}
}
公共类profileFragment扩展了片段{
列表视图列表;
TextView t_name;
FirebaseAuth FirebaseAuth;
FirebaseFirestore商店;
字符串用户标识;
String[]viewList={“请求”、“地址”、“关于我们”、“联系我们”、“注销”};
公共档案片段(){
}
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater充气机,@null可查看组容器,
@可为空的捆绑包savedInstanceState){
视图v=充气机。充气(R.layout.activity_profile,容器,假);
firebaseAuth=firebaseAuth.getInstance();
fstore=FirebaseFirestore.getInstance();
userId_s=firebaseAuth.getCurrentUser().getUid();
列表=v.findviewbyd(R.id.list);
t_name=v.findViewById(R.id.name_profile);
//从fstore获取用户名称
最终文档参考df=fstore.collection(“sales”).document(用户ID);
addSnapshotListener(新的EventListener(){
@凌驾
public void onEvent(@Nullable DocumentSnapshot值,@Nullable FirebaseFirestoreException
(错误){
t_name.setText(value.getString(“email”);
}
});
ArrayAdapter ArrayAdapter=新的ArrayAdapter(getActivity(),
android.R.layout.simple_list_item_1,viewList);
list.setAdapter(arrayAdapter);
list.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
开关(位置){
案例0:
请求();
返回;
案例1:
地址();
返回;
案例2:
关于我们();
返回;
案例3:
联系我们();
返回;
案例4:
签出();
返回;
}
}
关于_us()的隐私无效{
Toast.makeText(getActivity(),“关于我们的很快”,Toast.LENGTH_SHORT.show();
}
私人无效联系人_us(){
Toast.makeText(getActivity(),“尽快与我们联系”,Toast.LENGTH_SHORT.show();
}
专用无效地址(){
Toast.makeText(getActivity(),“欢迎辞”,Toast.LENGTH_SHORT.show();
}
私有无效请求(){
requestFragment fn=新的requestFragment();
FragmentTransaction ft=getFragmentManager().beginTransaction();
ft.更换(R.id.框架集装箱,fn);
ft.commit();
}
私人作废注销(){
FirebaseAuth.getInstance().signOut();
Intent Intent=新的Intent(getActivity(),login_Activity.class);
星触觉(意向);
}
});
返回v;
}
}
以及在logcat中看到后的错误:
如何解决这些问题
谢谢使用getFragmentManager()只是一个警告,但这不是真正的问题,您将获得错误和空快照。 您可以在LogCat中验证并显示错误
final DocumentReference df = fstore.collection("sales").document(userId_s);
df.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException
error) {
if (error!=null){
Log.d(TAG,"Error: "+e.getMessage());
} else {
t_name.setText(value.getString("email"));
}
}
})
final DocumentReference df=fstore.collection(“sales”).document(userId);
addSnapshotListener(新的EventListener(){
@凌驾
public void onEvent(@Nullable DocumentSnapshot值,@Nullable FirebaseFirestoreException
(错误){
if(错误!=null){
Log.d(标记“Error:+e.getMessage());
}否则{
t_name.setText(value.getString(“email”);
}
}
})
应该使用getFragmentManager()
而不是getFragmentManager()
,因为它在API级别28中被弃用。在堆栈溢出时,不要显示代码和文本的图片。将文本复制到问题中并格式化,以便阅读、复制和搜索。