1

Im getting error when i try to run the below activity. The activity has a custom navigation drawer. I'm new to android .... The error is occuring at places where actionbar is involved.

Im getting error when i try to run the below activity. The activity has a custom navigation drawer. I'm new to android .... The error is occuring at places where actionbar is involved.

Im getting error when i try to run the below activity. The activity has a custom navigation drawer. I'm new to android .... The error is occuring at places where actionbar is involved.

Activity

import android.app.ActionBar;
import android.content.Context;
import android.support.v4.widget.DrawerLayout;

import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class HomeActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    DrawerLayout drawer_layout;
    ListView drawer_list;
    ActionBarDrawerToggle drawer_toggle;
    ActionBar abar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        drawer_layout= (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer_list= (ListView) findViewById(R.id.drawer_list);
        drawer_list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1));
        drawer_list.setOnItemClickListener(this);
        abar=getActionBar();
        /*drawer_toggle = new ActionBarDrawerToggle(this, drawer_layout,
                R.drawable.tick, R.string.app_name, R.string.app_name) {

            *//** Called when a drawer has settled in a completely closed state. *//*
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getActionBar().setTitle("Yuride");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            *//** Called when a drawer has settled in a completely open state. *//*
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActionBar().setTitle("Yuride");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };*/
        abar.setHomeButtonEnabled(true);
        abar.setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    class Myadapter extends BaseAdapter
    {
        Context context;
        String[] drawer_items;
        public Myadapter(Context context)
        {
            this.context=context;
            drawer_items=context.getResources().getStringArray(R.array.drawer_items);
        }
        @Override
        public int getCount() {
            return drawer_items.length;
        }

        @Override
        public Object getItem(int position) {
            return drawer_items[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row=null;
            if(convertView==null)
            {
                LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row=inflater.inflate(R.layout.drawer_row,parent,false);
            }
            else
            {
                row=convertView;
            }
            ImageView drawer_icon= (ImageView) row.findViewById(R.id.drawer_icon);
            TextView drawer_text= (TextView) row.findViewById(R.id.drawer_text);
            drawer_text.setText(drawer_items[position]);
            drawer_icon.setImageResource(R.drawable.tick);
            return null;
        }
    }
}

xml

    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
    <ListView
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:id="@+id/drawer_list">
    </ListView>
</android.support.v4.widget.DrawerLayout>

Manifest

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.yusoft.yuride.yuride">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Entrance" />
        <activity android:name=".HomeActivity"></activity>
    </application>

</manifest>

Error

java.lang.RuntimeException: Unable to start activity ComponentInfo{in.yusoft.yuride.yuride/in.yusoft.yuride.yuride.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2622)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2683)
   at android.app.ActivityThread.-wrap12(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:6066)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:770)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference
   at in.yusoft.yuride.yuride.HomeActivity.onCreate(HomeActivity.java:52)
   at android.app.Activity.performCreate(Activity.java:6584)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2575)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2683) 
   at android.app.ActivityThread.-wrap12(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:6066) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:770) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660) 
il_raffa
  • 5,090
  • 129
  • 31
  • 36
  • You need to use `getSupportActionBar()` instead of `getActionBar()`. – Mike M. Apr 24 '16 at 08:22
  • Possible duplicate of [getActionBar() returns null](http://stackoverflow.com/questions/6867076/getactionbar-returns-null) – Mike M. Apr 24 '16 at 08:24
  • i have already done that the and still getting error!!!! –  Apr 24 '16 at 08:24
  • Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference at in.yusoft.yuride.yuride.HomeActivity.onCreate(HomeActivity.java:52) –  Apr 24 '16 at 08:26
  • You should've mentioned that in your question, instead of the copy/paste job. Anyway, the `AppTheme` is probably removing the `ActionBar`, then. – Mike M. Apr 24 '16 at 08:28
  • share paste the styles.xml here. You should have ```Theme.AppCompat.Light.DarkActionBar``` as your parent theme – Zeeshan Shabbir Apr 24 '16 at 08:37
  • I have changeTheme to AppCompat.Light. –  Apr 24 '16 at 08:39
  • Caused by: android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Class is not a View android.app.ActionBar Caused by: android.view.InflateException: Binary XML file line #6: Class is not a View android.app.ActionBar Caused by: java.lang.ClassCastException: class android.app.ActionBar cannot be cast to android.view.View –  Apr 24 '16 at 08:39
  • what is the error? – Zeeshan Shabbir Apr 24 '16 at 08:40
  • zeeshan the error is posted above –  Apr 24 '16 at 08:41
  • 1
    Get rid of the `ActionBar` in your layout. That answer was not correct. – Mike M. Apr 24 '16 at 08:41
  • mike said it already – Zeeshan Shabbir Apr 24 '16 at 08:42
  • bro,removed it and app runs ....but navigation drawer is not showing –  Apr 24 '16 at 08:47
  • You need to un-comment the `drawer_toggle = ...` block, set the toggle as the `DrawerListener` on `drawer_layout`, and call `drawer_toggle.syncState()`. Also, you're using the wrong constructor for the v7 `ActionBarDrawerToggle`. Remove the `R.drawable.tick` argument. – Mike M. Apr 24 '16 at 08:50
  • removing it solved my problem –  Apr 24 '16 at 08:54
  • how to set toggle as drawer listener?plz –  Apr 24 '16 at 08:58
  • `drawer_layout.addDrawerListener(drawer_toggle);`. And have a look at [this post](http://stackoverflow.com/questions/36570450/clicking-hamburger-icon-on-toolbar-does-not-open-navigation-drawer/36570774#36570774). – Mike M. Apr 24 '16 at 09:00
  • Thanks for your valuable reply..Now navigation drawer is shown in actionbar...But it is not opening from actionbar. –  Apr 24 '16 at 09:09
  • Did you consult the post I linked in my last comment? – Mike M. Apr 24 '16 at 09:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110048/discussion-between-aswin-a-s-and-mike-m). –  Apr 24 '16 at 09:16

1 Answers1

0

The problem is in setting up ActionBar. Instead of

    abar=getActionBar();
    abar.setHomeButtonEnabled(true);
    abar.setDisplayHomeAsUpEnabled(true);

Write in XML:

   <android.support.v4.widget.DrawerLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/drawer_layout">

     <android.support.design.widget.AppBarLayout
       android:id="@+id/app_bar"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:fitsSystemWindows="true"
       android:theme="@style/MyMaterialTheme.AppBarOverlay">

         <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/MyMaterialTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

     <FrameLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     </FrameLayout>
     <ListView
         android:layout_width="240dp"
         android:layout_height="match_parent"
         android:layout_gravity="left"
         android:id="@+id/drawer_list">
     </ListView>
 </android.support.v4.widget.DrawerLayout>

And in Java Class right after setContentView(R.layout.activity_home);:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    if(getSupportActionBar()!=null){
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
Basu
  • 763
  • 8
  • 27