here is Drawer activity code sliding:
public class sliding extends Activity {
// Within which the entire activity is enclosed
private DrawerLayout mDrawerLayout;
// ListView represents Navigation Drawer
private ListView mDrawerList;
// ActionBarDrawerToggle indicates the presence of Navigation Drawer in the
// action bar
private ActionBarDrawerToggle mDrawerToggle;
// Title of the action bar
private String mTitle = "";
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_sliding);
}
void Drawer() {
mTitle = "Care&Cure";
getActionBar().setTitle(mTitle);
// Getting reference to the DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.drawer_list);
// Getting reference to the ActionBarDrawerToggle
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
/** Called when drawer is closed */
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
/** Called when a drawer is opened */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("Care&Cure");
invalidateOptionsMenu();
}
};
// Setting DrawerToggle on DrawerLayout
mDrawerLayout.setDrawerListener(mDrawerToggle);
// Creating an ArrayAdapter to add items to the listview mDrawerList
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getBaseContext(), R.layout.drawer_list_item, getResources()
.getStringArray(R.array.menus));
// Setting the adapter on mDrawerList
mDrawerList.setAdapter(adapter);
// Enabling Home button
getActionBar().setHomeButtonEnabled(true);
// Enabling Up navigation
getActionBar().setDisplayHomeAsUpEnabled(true);
// Setting item click listener for the listview mDrawerList
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
and for main activity MainActivty:
public class MainActivity extends sliding implements OnClickListener {
ImageButton hosp, doc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Drawer();
hosp = (ImageButton) findViewById(R.id.btn_hosp);
doc = (ImageButton) findViewById(R.id.btn_doc);
hosp.setOnClickListener(this);
doc.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btn_doc) {
Intent dc = new Intent(MainActivity.this, sliding.class);
startActivity(dc);
}
activity_main.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" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bacground" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<!-- 2 columns -->
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" >
<ImageButton
android:id="@+id/btn_hos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector"
android:contentDescription="@string/image"
android:src="@drawable/hos" />
<ImageButton
android:id="@+id/btn_doc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector"
android:contentDescription="@string/image"
android:src="@drawable/doc" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" >
<ImageButton
android:id="@+id/btn_lab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector"
android:contentDescription="@string/image"
android:src="@drawable/lab" />
<ImageButton
android:id="@+id/btn_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selector"
android:contentDescription="@string/image"
android:src="@drawable/b" />
</TableRow>
</TableLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>
logcat:
08-26 14:25:24.268: W/dalvikvm(17531): threadid=1: thread exiting with uncaught exception (group=0x40aa8228)
08-26 14:25:24.268: E/AndroidRuntime(17531): FATAL EXCEPTION: main
08-26 14:25:24.268: E/AndroidRuntime(17531): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.careandcure/com.example.careandcure.MainActivity}: java.lang.NullPointerException
08-26 14:25:24.268: E/AndroidRuntime(17531): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
08-26 14:25:24.268: E/AndroidRuntime(17531): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
08-26 14:25:24.268: E/AndroidRuntime(17531): at android.app.ActivityThread.access$600(ActivityThread.java:139)
08-26 14:25:24.268: E/AndroidRuntime(17531): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
08-26 14:25:24.268: E/AndroidRuntime(17531): at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 14:25:24.268: E/AndroidRuntime(17531): at android.os.Looper.loop(Looper.java:156)
08-26 14:25:24.268: E/AndroidRuntime(17531): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-26 14:25:24.268: E/AndroidRuntime(17531): at dalvik.system.NativeStart.main(Native Method)
08-26 14:25:24.268: E/AndroidRuntime(17531): Caused by: java.lang.NullPointerException
08-26 14:25:24.268: E/AndroidRuntime(17531): at com.example.careandcure.sliding.Drawer(sliding.java:78)
08-26 14:25:24.268: E/AndroidRuntime(17531): at com.example.careandcure.MainActivity.onCreate(MainActivity.java:20)
08-26 14:25:24.268: E/AndroidRuntime(17531): at android.app.Activity.performCreate(Activity.java:4538)
08-26 14:25:24.268: E/AndroidRuntime(17531): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
08-26 14:25:24.268: E/AndroidRuntime(17531): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
i am following Android Navigation Drawer with multiple activites to create drawer in multiple activities its working fine in sliding but not in main activity..its giving null pointer exception
. how to resolve it.... thanks in advance
You're calling setContentView()
in the class that overrides sliding
, where you pass a new xml file to be inflated (R.layout.activity_main
). My guess is this xml file doesn't contain the layouts you require for your sliding
Activity
.
The example you are following is not a good one. You are trying have an Activity which hosts the NavigationDrawer, and then extend that Activity. The concept is fine, but your execution is wrong. Your slider class needs to be able to create and display a NavigationDrawer
and still remain extensible, so you need to write it in such a way that overriding standard methods (like onCreate()
and calling setContentView()
) does not impact on the slider class's ability to display the NavigationDrawer
.
I would suggest creating the NavigationDrawer
in a fragment, and inflating that fragment in the onCreate()
of your sliding Activity
. That way the NavigationDrawer
is fully contained within the fragment, and you don't have worry about which methods you override and what you do with them.