NullPointerException when setting listview adapter (DrawerLayout) [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I have a fragment layout which is the drawer and looks like this:
fragment_overview.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffe887" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="multipleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
And the java class for that fragment:
OverviewFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_overview, container, false);
mlistOptions = getResources().getStringArray(R.array.list_options);
mDrawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
mDrawerList = (ListView) getActivity().findViewById(R.id.left_drawer);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.simple_list_item_1, mlistOptions));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
return root;
}
With this code in the resources folder:
<string-array name="list_options">
<item>Ship</item>
<item>Map</item>
<item>Mail</item>
<item>Settings</item>
<item>Logout</item>
</string-array>
And this as a list item:
simple_list_item_1.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
android:textColor="#android:color/black"
android:textStyle="italic">
However when the setAdapter method runs in the java class it generates this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
What could have actually gotten wrong in that method call? It is a fragment so we need getActivity as the first parameter, the layout file is just a reference to a simple file and the optionslist is just an arraylist from the resources folder.. what can be null?
Thanks in advance!

try this, instead of using getActivity use root were you inflate your layout because your component now in root view.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_overview, null);
mlistOptions = getResources().getStringArray(R.array.list_options);
mDrawerLayout = (DrawerLayout) root.findViewById(R.id.drawer_layout);
mDrawerList = (ListView) root.findViewById(R.id.left_drawer);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.simple_list_item_1, mlistOptions));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
return root;
}

you should populate your drawer list in your main activity. Put this code in your activity's onCreate method.
Then in your fragment's onCreateView you shouuld inflate another Layout that will be put in your (main activity's) FrameLayout.

Try replacing your
mDrawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
mDrawerList = (ListView) getActivity().findViewById(R.id.left_drawer);
with
mDrawerLayout = (DrawerLayout) root.findViewById(R.id.drawer_layout);
mDrawerList = (ListView) root.findViewById(R.id.left_drawer);

Related

Navigation Drawer not loading layout with RecyclerView

I have my Events.java and event.xml layout, which has a recycler view & works fine when if I load the Event class directly. It looks like this:
However, if I load the navigation drawer, which then load the event.xml file, then I get the following screens with this error: "No adapter attached; skipping layout".
My issues is that why is the navigation menu not working and the event layout file not loading? As for my code, I think the following might be helpful.
Events.java
#Override
protected void onCreate(Bundle savedInstanceState) {
...
RecyclerView recyclerView = findViewById(R.id.events_rv_list);
EventsAdapter eventsAdapter = new EventsAdapter(this, eList, this);
recyclerView.setAdapter(eventsAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
events.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".Events"
android:background="#ffff6c">
<android.support.v7.widget.RecyclerView
android:id="#+id/events_rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
NavigationDrawer.java
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
...
Okay, I've found the solution. The error was that the adapter was not attached to the RecyclerView.
In the first picture, the adapter worked because it was stored in a normal layout file (non fragment) class, so the onCreate was being called from the events.java. Events.java then loaded the layout (events.xml) and also attached the adapters to the RecyclerView like so:
RecyclerView recyclerView = view.findViewById(R.id._events_recyclerview);
ADAPTE_CLASS adapter = new ADAPTE_CLASS(PASS DATA IF NECESSARY);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
Now same thing can be applied in a Fragment where you can add the above code to onCrateView method (with relevant class names & ids). So that's how you set a recyclerview adapter in a fragnent.
Hope that helps.

ListView in Fragment doesn't show anything

I know that this question has been asked a lot of times already but I have went to all of the answers and none of them worked for me.
So, issue with the App is that I am trying to populate an ArrayList into a ListView using an ArrayAdapter and the app MainActivity doesn't show anything but white screen when launched.
Here's
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static class PlaceholderFragment extends Fragment{
private ArrayAdapter<String> mForecastAdapter;
public PlaceholderFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
String[] forecastArray = {
"Today - Sunny - 88/63",
"Tomorrow - Foggy - 70/46",
"Weds - Cloudy - 72/63",
"Thurs - Rainy - 64/51",
"Fri - Foggy - 70/46",
"Sat - Sunny - 76/68"
};
List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
mForecastAdapter = new ArrayAdapter<String>(
//The current context (current activity)
getActivity(),
//ID of list item layout
R.layout.list_item_forecast,
//id of the textView to populate
R.id.list_item_forecast_textview,
//data to populate
weekForecast
);
return rootView;
}
}
fragment_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.gautamhans.sunshine.app.MainActivityFragment"
tools:showIn="#layout/activity_main">
<ListView
android:id="#+id/listview_forecast"
android:layout_width="match_parent"
android:layout_height="667dp"
/>
</FrameLayout>
Here's list_item_forecast.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:id="#+id/list_item_forecast_textview"
>
</TextView>
Of course the activity is not showing anything, you're not placing the fragment inside your activity! Use this in your activity's onCreate():
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new PlaceholderFragment(), "fragment_tag").commit();
Where R.id.container is a layout inside your main_activity_layout file (empty layout)
UPDATE:
have these 2 file:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
fragment_main:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.gautamhans.sunshine.app.MainActivityFragment"
tools:showIn="#layout/activity_main">
<ListView
android:id="#+id/listview_forecast"
android:layout_width="match_parent"
android:layout_height="667dp"
/>
</FrameLayout>
keep the 3° layout file, it's ok.
Now, in your activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new PlaceholderFragment(), "fragment_tag").commit();
}
also:
mForecastAdapter = new ArrayAdapter<String>(
//The current context (current activity)
getActivity(),
//ID of list item layout
R.layout.list_item_forecast,
//id of the textView to populate
R.id.list_item_forecast_textview,
//data to populate
weekForecast
);
//move this after the initialization of adapter
listView.setAdapter(mForecastAdapter);
When you set the ListView adapter,the adapter's reference is null and you set the null adapter for ListView.You should create the adapter before set ListView adapter.
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
mForecastAdapter = new ArrayAdapter<String>(
//The current context (current activity)
getActivity(),
//ID of list item layout
R.layout.list_item_forecast,
//id of the textView to populate
R.id.list_item_forecast_textview,
//data to populate
weekForecast
);
listView.setAdapter(mForecastAdapter);
Is your PlaceHolderFragment within your MainActivity.java file?
If so, try separating it on its own file PlaceHolderFragment.java..
As far as I know, java only allows one public class per source file.. That maybe the reason of your problem..

Android Programming Error "No view found for id for fragment " with Google Maps

So I am using the default Navigation Drawer for my Android Application. What I'm trying to do is linking a button to a fragment I created.
In the fragment I created, I am trying to open up Google Maps.
Whenever I click on the button however, I get this crash:
This is the generated error (i have no syntax errors):
E/FragmentManager: No view found for id 0x7f0d0089 (com.example.x.x:id/mapView) for fragment LocationFragment{35b5044 #1 id=0x7f0d0089}
This is the code from the default menu (I try to only put the needed code):
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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);
FragmentManager fm = getFragmentManager();
}
In the same default Navigation drawer, we also have the onNavigationItemSelected(MenuItem item) function, that does an action when you click on the menu item, this is where I make the FragmentManager call my LocationFragment:
public boolean onNavigationItemSelected(MenuItem item) {
FragmentManager fm = getFragmentManager();
int id = item.getItemId();
if (id == R.id.nav_camara) {
// Handle the camera action
} else if (id == R.id.nav_share) {
fm.beginTransaction().replace(R.id.mapView, new LocationFragment()).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
And here we have the LocationFragment Java class (witht he most important function included):
public class LocationFragment extends Fragment {
MapView mMapView;
private GoogleMap googleMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate and return the layout
View v = inflater.inflate(R.layout.location_fragment, container,
false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mMapView.getMap();
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;
// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello
return v;
}
The layout file (xml) of the LocationFragment (called location_fragment.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
I do not understand why I get the error, since I have checked all ID's in all classes for maybe 100 times. I seem be stuck here. Any help would be appreciated
Thanks in advance
Added activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<include layout="#layout/app_bar_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView android:id="#+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main" app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
From this , it says: Replace method replaces an existing fragment that was added to a container.
public abstract FragmentTransaction replace (int containerViewId, Fragment fragment)
Where containerViewId is a container. I dont think MapView is a container. So instead of:
fm.beginTransaction().replace(R.id.mapView, new LocationFragment()).commit();
It should be:
fm.beginTransaction().replace(R.id.mapViewContainer, new LocationFragment()).commit();
Where mapViewContainer is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/mapViewContainer">
Hope this help.

In Android SDK/Java, how do I manipulate XML Views(imagebuttons, textviews) from a method in a fragment?

Solved! Thanks everyone for your help.
I am struggling with Fragments as a concept, and especially seem stuck on this one thing. What I'm trying to do is, using my fragment, manipulate its own layout's data. Every time I try to access an ImageButton from within the Fragment, it crashes the application. It works fine from the activity. Am I just misunderstanding Fragments fundamentally?
Code(cut down for size)-
This is the beginning of the activity my fragment is called from:
Display.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragmentClass MyFragment = new MyFragmentClass();
fragmentTransaction.add(R.id.fragment_container, MyFragment);
fragmentTransaction.commit();
The XML for that Activity:
activity_display.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.mycompanyname.myprojectname.Display"
android:id="#+id/display_layout">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container">
</FrameLayout>
</RelativeLayout>
The Fragment
MyFragmentClass.java
public class MyFragmentClass extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout_screen, container, false);;
}
public void MethodTest()
{
}
}
The Fragment's XML file:
fragment_layout_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_layout_screen"
tools:context="com.mycompanyname.myprojectname.MyFragmentClass">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/my_button"
android:clickable="true"
android:onClick="buttonPress"
android:src="#drawable/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
Basically I had all of this originally in Display.java, but wanted to add fragments, so I'm trying to move stuff out and into fragments, but I still need the ability to manipulate the xml info, I just can't.
From inside the Display.java activity I can easily call the ImageButton like this:
ImageButton myButton = (ImageButton) findViewById(R.id.my_button)
but if I do the same from MethodTest in the fragment, the app crashes.
I've searched many suggestions on here, trying various solutions from here: findViewById in Fragment but none of those seemed to work.
I've been reading through this for the setup: http://developer.android.com/guide/components/fragments.html and can't seem to find what I'm doing wrong.
Any help would be appreciated, and if you have any questions about my setup, please ask.
declare on fragment private View rootView;
On the onCreateView(...) set rootView = inflater.Inflate(..); and then access the imagebutton as ImageButton mButton =(ImageButton) rootView.findViewById(..);
Do like this in fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gridview, container, false);
GridView gridview = (GridView) view.findViewById(R.id.grid);
return view ;

Using a DrawerLayout along with a ListView

I'm trying to write an android app with a listview of a few different items and also a drawerlayout that you can drag from the left side of the screen. Here's what i mean....
This is what the main screen looks like:
And here's what the drawer menu thing looks like:
The problem i'm having is that when I open the side drawer menu thing and tap an option it doesn't work and the menu just closes. However i'm able to interact with the main listview page. Here's what my code looks like:
String[] homeArray = { "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test", "Test" };
private ListView homeListView;
private ArrayAdapter arrayAdapter;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mDrawerTitles;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mDrawerTitles = getResources().getStringArray(R.array.Menu);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mDrawerTitles));
And the activity_main.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<FrameLayout
android:id="#+id/content_frame"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_height="match_parent"
android:layout_width="240dp"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/darker_gray"
android:dividerHeight="1dp"
android:background="#111"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ListActivity" >
<ListView
android:id="#+id/homeListView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
I have a feeling the problem is in the xml but i'm not sure.
Please post the full code of your Activity that holds the NavigationDrawer.
Furthermore, I would strongly recommend (and so would Google) that you use Fragments for the individual NavigationDrawer sections ("Settings", "Submit Bug" and "Help" in your case).
Do not put all the layout components into the layout file of your Activity.
The layout therefore should look like the following:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
The FrameLayout content_frame is where your Fragment that holds the ListView will go into.
Simply create a custom Fragment (or use a ListFragment) that contains the ListView you displayed in your picture and insert it into the "content_frame".
Use this to replace Fragments inside the content frame: (when you switch your section)
/** Swaps fragments in the main content view */
private void selectItem(int position) {
// Create a new fragment and specify the planet to show based on position
Fragment fragment = new YourListFragment(); // this fragment contains the list with all the "test" items
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
// Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
An Example of how your Fragment could look like: YourListFragment.java
The layout file "list_fragment.xml" simply contains whatever layout components you want. For example a ListView. Initialize an populate the ListView inside the Fragments onCreateView() method.
public class YourListFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.list_fragment, container, false);
return rootView;
}
}
All taken from Googles example on how to create a NavigationDrawer:
http://developer.android.com/training/implementing-navigation/nav-drawer.html

Categories