单击导航项时,我无法更改片段。它只发生在我的FragmentTwo中。提交会导致错误,应用程序就会关闭。拜托,有人可以帮帮我吗?错误在哪里?
主要代码
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container body-content">
<div class="page-header">
<div class="form-group">
<fieldset>
<form action="" id="createForm" class="form-group has-feedback" method="post">
<div class="form-horizontal">
<div class="col-lg-12">
<div class="form-group">
<label class="col-md-9 control-label" for="Application">Application</label>
<div class="col-md-4">
<input id="ApplicationID" name="Application" required="required" type="text" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-md-9 control-label" for="EscID">EM</label>
<div class="col-md-4">
<input id="EscID" name="EM" type="text" placeholder="(If Applicable)" class="form-control" />
</div>
</div>
</div>
</div>
</form>
<!--END OF FORM ^^-->
</fieldset>
</div>
</div>
</div>
FRAGMENT ONE CODE
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inicio_admin);
// Implementação do navigation view
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
if(savedInstanceState == null) {
FragmentOne frag1 = new FragmentOne();
FragmentTwo frag2 = new FragmentTwo();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.layoutFundo, frag1, "frag1");
ft.commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(toggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
FragmentOne frag1 = new FragmentOne();
FragmentTwo frag2 = new FragmentTwo();
FragmentTransaction ft = fm.beginTransaction();
if (item.getItemId() == R.id.nav_menu_1) {
ft.replace(R.id.layoutFundo, frag1, "frag1");
ft.addToBackStack("pilha");
showMessage("IF", "Entrou no if");
Toast.makeText(InicioAdmin.this, "Novo", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == R.id.nav_menu_2) {
ft.replace(R.id.layoutFundo, frag2, "frag2");
ft.addToBackStack("pilha");
showMessage("ELSEIF", "Entrou no Elseif");
Toast.makeText(InicioAdmin.this, "Outro", Toast.LENGTH_SHORT).show();
} else {
ft.replace(R.id.layoutFundo, frag1, "frag1");
ft.addToBackStack("pilha");
showMessage("ELSE", "Entrou no else");
}
ft.commit();
/* switch (item.getItemId()) {
case R.id.nav_menu_1:
break;
case R.id.nav_menu_2:
break;
default:
break;
}*/
DrawerLayout dl = (DrawerLayout) findViewById(R.id.drawerLayout);
if (dl.isDrawerOpen(GravityCompat.START))
dl.closeDrawer(GravityCompat.START);
return false;
}
// Método showMessage
public void showMessage (String title, String message)
{
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
碎片两个代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_one, null);
TextView tv = (TextView) view.findViewById(R.id.textView1);
tv.setText("Fragmento 1");
return (view);
}
这两个类扩展了Fragment
答案 0 :(得分:0)
我认为你在片段中返回null,这就是你得到错误的原因。请在片段中添加视图并运行它。希望它会有所帮助。