0

I want create an BaseActivity with Navigation Drawer for extends other activities from this. (Use one Drawer for all activities)

I searched and tried many codes like this link but i still have:

Unable to start activity (NullPointExeption) error!

Also I write this code in MainActivity:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
super.onCreateDrawer();

but it don't work :(

BaseActivity:

public abstract class BaseActivity extends Activity
{
    public DrawerLayout drawerLayout;
    public String[] layers;
    private ActionBarDrawerToggle drawerToggle;

    protected void onCreateDrawer()
    {
        // R.id.drawer_layout should be in every activity with exactly the same id.
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_drawer, 0, 0) 
        {
            public void onDrawerClosed(View view) 
            {
                getActionBar().setTitle(R.string.app_name);
            }

            public void onDrawerOpened(View drawerView) 
            {
                getActionBar().setTitle(R.string.hello_world);
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
    }

}

MainActivity:

public class MainActivity extends BaseActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        super.onCreateDrawer();
    }
}

drawer_activity.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">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <!-- Put what you want as your normal screen in here, you can also choose for a linear layout or any other layout, whatever you prefer -->
</FrameLayout>
<!-- 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"/>

Logcat:

   09-07 07:12:47.308: E/AndroidRuntime(2620): FATAL EXCEPTION: main
09-07 07:12:47.308: E/AndroidRuntime(2620): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
09-07 07:12:47.308: E/AndroidRuntime(2620):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-07 07:12:47.308: E/AndroidRuntime(2620):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-07 07:12:47.308: E/AndroidRuntime(2620):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-07 07:12:47.308: E/AndroidRuntime(2620):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-07 07:12:47.308: E/AndroidRuntime(2620):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 07:12:47.308: E/AndroidRuntime(2620):     at android.os.Looper.loop(Looper.java:137)
09-07 07:12:47.308: E/AndroidRuntime(2620):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-07 07:12:47.308: E/AndroidRuntime(2620):     at java.lang.reflect.Method.invokeNative(Native Method)
09-07 07:12:47.308: E/AndroidRuntime(2620):     at java.lang.reflect.Method.invoke(Method.java:511)
09-07 07:12:47.308: E/AndroidRuntime(2620):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-07 07:12:47.308: E/AndroidRuntime(2620):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-07 07:12:47.308: E/AndroidRuntime(2620):     at dalvik.system.NativeStart.main(Native Method)
09-07 07:12:47.308: E/AndroidRuntime(2620): Caused by: java.lang.NullPointerException
09-07 07:12:47.308: E/AndroidRuntime(2620):     at com.example.test.NavigationActivity.onCreateDrawer(NavigationActivity.java:38)
09-07 07:12:47.308: E/AndroidRuntime(2620):     at com.example.test.MainActivity.onCreate(MainActivity.java:11)
09-07 07:12:47.308: E/AndroidRuntime(2620):     at android.app.Activity.performCreate(Activity.java:5008)
09-07 07:12:47.308: E/AndroidRuntime(2620):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-07 07:12:47.308: E/AndroidRuntime(2620):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

what's the solution?! Thanks.

Community
  • 1
  • 1
Erfun
  • 1,079
  • 2
  • 11
  • 26

2 Answers2

2

The NPE could be easily caused by getActionBar() depending on the version of the Android and your manifest's configuration. I strongly suggest you to extend AppCompatActivity instead of Activity, and use getSupportActionBar() instead of getActionBar(). Be aware that both can return null, depending of the Theme you selected on your manifest.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • i use eclipse and eclipse don't have Android AppCompat v22.01 Library! how can i download it? – Erfun Sep 07 '15 at 08:32
0

Your getting error in onCreateDrawer() method. It can be because of:

  1. Instead of extending Activity use AppCompatActivity

  2. Instead of this line

drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_drawer, 0, 0)

Make it like this

drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, 0, 0)

  1. Check your theme, Use a theme which can support ActionBar Here is the Android Documentation link Styling Action Bar
Pankaj
  • 7,908
  • 6
  • 42
  • 65
  • AppCompatActivity is undefined for eclipse! i add android:theme="@style/Theme.AppCompat.Light.DarkActionBar" to application tag in manifest but yet the problem remains – Erfun Sep 07 '15 at 08:34
  • You have to add the **Support v7 library** also. – Pankaj Sep 07 '15 at 09:31
  • I can't find Android Appcompat Support Library v7 ( 22.01 or higher ) for download! – Erfun Sep 07 '15 at 09:36
  • You can download it from your **SDK Managaer** you can see **Android Support Library** option in that which you have to update to latest. – Pankaj Sep 07 '15 at 09:40
  • Developer.Android.com and related google website not accessible for Iran :| I want direct link for download Android Support Library v7 (22.01 or higher) – Erfun Sep 07 '15 at 10:03