我正在使用导航栏,其中包含菜单Home,Hospital,Doctors和Restaurant,并使用Fragment将一个片段移动到其他片段,但是当点击Doctor菜单时,应用程序停止了。
MainActivity.java
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
FragmentTransaction fragmentTransaction;
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container,new BlankFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("GApp");
navigationView=(NavigationView)findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.home:
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new BlankFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home");
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.hospital:
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new HospitalFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Hospitals");
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.doctor:
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new DoctorFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Doctors");
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.restaurent:
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container,new restaurent());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Restaurents");
item.setChecked(true);
drawerLayout.closeDrawers();
break;
}
return true;
}
});
drawerLayout.addDrawerListener(actionBarDrawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
}
DoctorFragment.java
public class DoctorFragment extends Fragment {
ListView listView;
String[] doctors={"Physician","Child Specialist","Dentist","Neurologist"};
public DoctorFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
listView=(ListView)getActivity().findViewById(R.id.listview);
ArrayAdapter adapter=new ArrayAdapter<String>(getActivity(),R.layout.list_view_custom_layout,doctors);
listView.setAdapter(adapter);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_doctor, container, false);
}
}
那么,这是你的布局吗?
<FrameLayout
xmlns:android="schemas.android.com/apk/res/android"
xmlns:tools="schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nk.gapp.DoctorFragment">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</FrameLayout>
答案 0 :(得分:0)
尝试对您的片段进行此更改:由于您尚未共享logcat,因此很难指出真正的原因:首先膨胀您的视图,然后按ID查找listview,像往常一样设置适配器,然后最终返回视图
FALSE