Reputation: 89
I want to create a navigation drawer menu opened by sliding from left to right and I did it by a google developer tutorial, but the tutorial showed how to create the navigation drawer and how to set the item from the menu to open new fragment layouts, but it's not what I wanted.. what I want is to open new activities with the item from the navigation drawer menu, and I did it too, but when the Sliding.java activity has to open, my app crashes.. Sliding.java is the activity which contains the codes for the menu .
Sliding.java:
package com.orar.cngcnasaud;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Sliding extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
private void selectItem(int i) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return false;
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@SuppressWarnings("unused")
private void selectItem1(final int position) {
switch(position) {
case 1:
Intent a = new Intent(this, MainActivity.class);
startActivity(a);
break;
case 2:
Intent b = new Intent(this, Profesori.class);
startActivity(b);
break;
default:
}
}
}
I have no errors when I run it, but it's still crashing..
EDIT:
LOGCAT:
02-17 16:29:58.496: I/dalvikvm(322): Could not find method com.orar.cngcnasaud.Sliding.getActionBar, referenced from method com.orar.cngcnasaud.Sliding.onCreate
02-17 16:29:58.496: W/dalvikvm(322): VFY: unable to resolve virtual method 5311: Lcom/orar/cngcnasaud/Sliding;.getActionBar ()Landroid/app/ActionBar;
02-17 16:29:58.496: D/dalvikvm(322): VFY: replacing opcode 0x6e at 0x0058
02-17 16:29:58.496: D/dalvikvm(322): VFY: dead code 0x005b-0087 in Lcom/orar/cngcnasaud/Sliding;.onCreate (Landroid/os/Bundle;)V
02-17 16:29:58.504: I/dalvikvm(322): Could not find method com.orar.cngcnasaud.Sliding.getActionBar, referenced from method com.orar.cngcnasaud.Sliding.setTitle
02-17 16:29:58.504: W/dalvikvm(322): VFY: unable to resolve virtual method 5311: Lcom/orar/cngcnasaud/Sliding;.getActionBar ()Landroid/app/ActionBar;
02-17 16:29:58.504: D/dalvikvm(322): VFY: replacing opcode 0x6e at 0x0002
02-17 16:29:58.504: D/dalvikvm(322): VFY: dead code 0x0005-000b in Lcom/orar/cngcnasaud/Sliding;.setTitle (Ljava/lang/CharSequence;)V
02-17 16:29:58.534: D/AndroidRuntime(322): Shutting down VM
02-17 16:29:58.534: W/dalvikvm(322): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-17 16:29:58.554: E/AndroidRuntime(322): FATAL EXCEPTION: main
02-17 16:29:58.554: E/AndroidRuntime(322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.orar.cngcnasaud/com.orar.cngcnasaud.Sliding}: java.lang.NullPointerException
02-17 16:29:58.554: E/AndroidRuntime(322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.os.Looper.loop(Looper.java:123)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-17 16:29:58.554: E/AndroidRuntime(322): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 16:29:58.554: E/AndroidRuntime(322): at java.lang.reflect.Method.invoke(Method.java:521)
02-17 16:29:58.554: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-17 16:29:58.554: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-17 16:29:58.554: E/AndroidRuntime(322): at dalvik.system.NativeStart.main(Native Method)
02-17 16:29:58.554: E/AndroidRuntime(322): Caused by: java.lang.NullPointerException
02-17 16:29:58.554: E/AndroidRuntime(322): at com.orar.cngcnasaud.Sliding.onCreate(Sliding.java:39)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-17 16:29:58.554: E/AndroidRuntime(322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-17 16:29:58.554: E/AndroidRuntime(322): ... 11 more
02-17 16:30:06.275: I/Process(322): Sending signal. PID: 322 SIG: 9
sliding.xml:
<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"
android:background="@drawable/bff">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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>
Upvotes: 2
Views: 3024
Reputation: 8539
I had the same problem. So I solved it using the following steps. It ll be useful to others.
The problem is because the compiler searching drawer_layout
in the activity_main.xml
.
setContentView(R.layout.activity_main);
//You added the view file as activity_main
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//While execution the compiler will search "drawer_layout" in the view file (activity_main.xml). But the drawer_layout is in sliding.xml.
But there is no "drawer_layout"
is added in the activity_main.xml
. Its added in the "sliding.xml"
So, To solve this problem we need to add the following in 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_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bff">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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"/>
<!-- The content in the activity_main.xml -->
</android.support.v4.widget.DrawerLayout>
Upvotes: 0
Reputation: 11337
You have a NullPointer here:
Caused by: java.lang.NullPointerException at com.orar.cngcnasaud.Sliding.onCreate(Sliding.java:39)
This seems to be your DrawerLayout (not sure about the line anyway):
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
Can you check/post your layout?
Upvotes: 1