hcnak hcnak - 2 months ago 17
Android Question

Error occurred in switching view in Fragment Navigation Drawer ,said no view found for fragment PlaceholderFragment

I had created a simple app via Android Studio using Navigation Drawer Fragment.

I was trying to show another view (like

layout_about.xml
) when user clicks an item of
ListView
.

So I use
setContentView()
in
public void onNavigationDrawerItemSelected(int position)
but when I run it, an error occurred :

FATAL EXCEPTION: mainProcess: com.akakanch.sample, PID: 28848
java.lang.IllegalArgumentException: No view found for id 0x7f0c0050 (com.akakanch.sample:id/container) for fragment PlaceholderFragment{250d0b42 #1 id=0x7f0c0050}


Here's my the only place I had change:

MainActivity.java

@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
setContentView(R.layout.layout_about);


}


So, how can I change the view correctly´╝č

Answer

it is because you are calling set content view from there. just set the layout you want to see as the layout of the fragment you are replacing and you will see it

inside the fragment:

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.layout_about,container,false);  
      //do your fragment stuff 
      return view;
    }

if you do this when you replace the fragment using fragment manager you will see the layout_about layout

Comments