Android fragments 片段之间的导航java.lang.IllegalStateException
我有一个导航抽屉和2个片段,默认情况下显示了1个片段并加载一个列表,当用户选择列表中的任何对象时,我想更改片段,但我不知道如何更改片段,我在抽屉中创建了一个方法,并在选择对象但得到java.lang.IllegalStateException时调用它,这是密码 主抽屉:Android fragments 片段之间的导航java.lang.IllegalStateException,android-fragments,Android Fragments,我有一个导航抽屉和2个片段,默认情况下显示了1个片段并加载一个列表,当用户选择列表中的任何对象时,我想更改片段,但我不知道如何更改片段,我在抽屉中创建了一个方法,并在选择对象但得到java.lang.IllegalStateException时调用它,这是密码 主抽屉: public class DrawerPrincipal extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedL
public class DrawerPrincipal extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,FragmentoPrincipalUsuario.OnFragmentInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_principal);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//cargar fragment principal usuario
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//fragmento del chofer
Fragment fragmento= null;
fragmento = new FragmentoPrincipalChofer();
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_main, fragmento)
.commit();
}
public void cambiarFragmento(){
Fragment fragmento;
fragmento = new PruebaUsuario();
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_main, fragmento)
.commit();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pantalla_principal_usuario, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.cuenta) {
Fragment fragmento= null;
fragmento = new OpcionDrawerUsuario();
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_main, fragmento)
.commit();
} else if (id == R.id.historial_viajes) {
Fragment fragmento= null;
fragmento = new FragmentoViajesAcumulados();
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_main, fragmento)
.commit();
} else if (id == R.id.contacto) {
String emailAdress[] = {"gastondelacruz@gmail.com"};
Intent email= new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL,emailAdress);
email.putExtra(Intent.EXTRA_SUBJECT,"Pon aqui el motivo de tu mail");
email.setType("text/plain");
email.putExtra(Intent.EXTRA_TEXT,"Su mensaje");
startActivity(email);
} else if (id == R.id.compartir) {
String texto="https://www.google.com.ar/?gfe_rd=cr&ei=9JuBVvmcGanX8gejsK_4CA&gws_rd=ssl";
Intent intento= new Intent();
intento.setAction(Intent.ACTION_SEND);
intento.putExtra(Intent.EXTRA_TEXT, texto);
intento.setType("text/plain");
startActivity(intento);
} else if (id == R.id.version) {
String prueba="aceptar";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("prueba") //
.setMessage("Esta es una prueba todavia no poseo el numero de version del producto") //
.setPositiveButton(prueba, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// TODO
dialog.dismiss();
}
}); //
builder.show();
} else if (id == R.id.salir) {
finish();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
抽屉布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".DrawerPrincipal"
tools:showIn="@layout/app_bar_drawer_principal">
<LinearLayout
android:id="@+id/content_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp"></LinearLayout>
</RelativeLayout>
这篇帖子很旧,但我还是会回复的 现在还不清楚出了什么问题。如果发布IllegalStateException的堆栈跟踪,可能会有所帮助 检查事项: 确保您没有将支持片段/活动与其备用版本混合
此外,您提到了从抽屉导航,但代码将向操作栏菜单添加按钮。不确定这是否是您想要的,但它们是不同的ui组件。请显示您的fragment+listadapter
DrawerPrincipal prueba=new DrawerPrincipal();
prueba.cambiarFragmento();