java.lang.IollegalStateException:键f0的片段不再存在:唯一id



我是android的初学者,我不明白为什么我会出错。当我点击主页按钮(底部导航栏(时,我会收到这个错误。当检索到这个值时,我使用共享的首选项来保存我的值,但当检索到该值时,就会出现错误。

MyActivityCode:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLastLocation();
binding.bottmNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.home1: //when i m clicking here
setFragment(homeFragment);
SharedPreferences shprefs = getSharedPreferences(prefs, MODE_PRIVATE);
SharedPreferences mprefs = getSharedPreferences(myprefs, MODE_PRIVATE);
String savedvalue = shprefs.getString("savedvalue", null);
String savedcountry = mprefs.getString("savedcountry", null);
if ((savedvalue != null && savedvalue.equals("India")) || savedcountry.equals("India")) {
newsModels.clear();
binding.bottmNav.getMenu().findItem(R.id.location).setTitle("India");
binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
setindianewsdata();
setindiashopdata();
setFragment(homeFragment);
} else if (savedvalue.equals("Usa") || savedcountry.equals("Usa")) {
newsModels.clear();
setusanewsdata();
setusashopdata();
setFragment(homeFragment);
binding.bottmNav.getMenu().findItem(R.id.location).setTitle("USA");
binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
}
return true;
case R.id.location:
final AlertDialog.Builder mbuilder = new AlertDialog.Builder(HomeActivity.this);
View v = getLayoutInflater().inflate(R.layout.dialog_layout, null);
Button okbutton = v.findViewById(R.id.okbutton);
final Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
ArrayAdapter < String > adapter = new ArrayAdapter < String > (HomeActivity.this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.countries_list));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
mbuilder.setView(v);
final AlertDialog dialog = mbuilder.create();
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
okbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences(prefs, MODE_PRIVATE).edit();
editor.putString("savedvalue", spinner.getSelectedItem().toString());
editor.commit();
String text = spinner.getSelectedItem().toString();
newsModels = new ArrayList < > ();
if (text.equals("India")) {
newsModels.clear();
setindianewsdata();
setindiashopdata();
setFragment(homeFragment);
binding.bottmNav.getMenu().findItem(R.id.location).setTitle("India");
binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
dialog.dismiss();
} else if (text.equals("Usa")) {
newsModels.clear();
setusanewsdata();
setusashopdata();
setFragment(homeFragment);
binding.bottmNav.getMenu().findItem(R.id.location).setTitle("USA");
binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
dialog.dismiss();
}
}
});
}
return true;
}
});
}

public void getLastLocation() {
if (checkPermissions()) {
if (isLocationEnabled()) {
progressBar.setVisibility(View.VISIBLE);
mFusedLocationClient.getLastLocation().addOnCompleteListener(
new OnCompleteListener < Location > () {
@Override
public void onComplete(@NonNull Task < Location > task) {
Location location = task.getResult();
if (location == null) {
requestNewLocationData();
} else {
countryname = findcountry(location.getLatitude(), location.getLongitude());
handlevalue(countryname);
if (countryname.equals("India")) {
newsModels.clear();
setindianewsdata();
setindiashopdata();
TypedArray socialimg = getResources().obtainTypedArray(R.array.social_image);
String[] socialname = getResources().getStringArray(R.array.social_name);
String[] socialhref = getResources().getStringArray(R.array.social_url);
socialModels = new ArrayList < > ();
for (int k = 0; k < socialimg.length(); k++) {
SocialModel socialModel = new SocialModel(socialimg.getResourceId(k, 0), socialname[k], socialhref[k]);
socialModels.add(socialModel);
}
SocialSingleton.getConstant().addNewsModel(socialModels);
setFragment(homeFragment);
binding.bottmNav.getMenu().findItem(R.id.location).setTitle("India");
binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
progressBar.setVisibility(View.GONE);
} else if (countryname.equals("USA")) {
TypedArray indianewslogo = getResources().obtainTypedArray(R.array.usnews);
setusanewsdata();
setusashopdata();
setFragment(homeFragment);
binding.bottmNav.getMenu().findItem(R.id.location).setTitle("USA");
binding.bottmNav.getMenu().findItem(R.id.home1).setChecked(true);
progressBar.setVisibility(View.GONE);
}
}
}
}
);
} else {
Toast.makeText(this, "Turn on location", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
} else {
requestPermissions();
}
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, fragment);
fragmentTransaction.commit();
}
}

当我点击主页按钮时,我会收到这个错误。不知道为什么。

java.lang.IllegalStateException: Fragment no longer exists for key f0: unique id 2b2916b8-40c2-4190-b2d7-e6440a7592eb
at androidx.fragment.app.FragmentManagerImpl.getFragment(FragmentManagerImpl.java:365)
at androidx.fragment.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:309)
at androidx.viewpager.widget.ViewPager.onRestoreInstanceState(ViewPager.java:1461)
at android.view.View.dispatchRestoreInstanceState(View.java:18042)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3859)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3865)
at android.view.View.restoreHierarchyState(View.java:18020)
at androidx.fragment.app.Fragment.restoreViewState(Fragment.java:548)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:907)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
at

在我们的viewpager 中设置setSaveEnabled((为false

相关内容

  • 没有找到相关文章

最新更新