Reputation: 1917
I am facing a problem of getting null in getSupportActionBar(), when setting toolbar as actionbar in an activity and setting the back button programmatically from supportActionBar, the exception I get is,
Attempt to invoke virtual method 'void android.app.ActionBar' on a null object reference
here is the xml view of the activity, the toolbar is included from another layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar_ftp"
android:id="@+id/include" />
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="@+id/include"
android:layout_alignParentStart="true" />
<RelativeLayout
android:layout_alignParentBottom="true"
android:id="@+id/below_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:background="@color/white">
<TextView
android:layout_below="@+id/line11"
android:id="@+id/txt_search_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_margin="16dp"
android:text="@string/search_by_Date"/>
<RelativeLayout
android:id="@+id/rl_cal_pick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_search_date"
>
<ImageView
android:id="@+id/icon_secheudel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/rlBoxtime"
android:layout_alignBottom="@+id/rlBoxtime"
android:src="@mipmap/ic_clock_red"
android:tint="@color/colorPrimary"/>
<RelativeLayout
android:id="@+id/rlBoxtime"
android:layout_toRightOf="@+id/icon_secheudel"
android:background="@drawable/blue_out_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="3dp"
android:paddingTop="3.5dp"
android:paddingBottom="3.5dp"
android:layout_marginLeft="0dp">
<TextView
android:id="@+id/txt_chosen_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:minWidth="30dp"
android:text=""/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_toRightOf="@+id/txt_chosen_date"
android:layout_centerVertical="true"
android:src="@mipmap/ic_calendar_schedule"
android:tint="@color/colorAccent"
android:layout_marginRight="2.5dp"/>
</RelativeLayout>
</RelativeLayout>
<TextView
android:id="@+id/btn_tommorow"
android:text="@string/tommorow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:gravity="center"
android:layout_alignTop="@+id/rl_cal_pick"
android:layout_alignBottom="@+id/rl_cal_pick"
android:layout_toRightOf="@+id/tvTOday"
android:textColor="@color/colorAccent"
android:padding="5dp"
android:background="@drawable/blue_out_line"
style="@style/borderless_blue_text_button"
/>
<TextView
android:id="@+id/tvTOday"
android:text="@string/today"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:gravity="center"
android:padding="5dp"
android:layout_marginLeft="10dp"
android:layout_alignTop="@+id/rl_cal_pick"
android:layout_alignBottom="@+id/rl_cal_pick"
android:layout_toRightOf="@+id/rl_cal_pick"
android:textColor="@color/colorAccent"
android:background="@drawable/blue_out_line"
android:layout_marginRight="8dp"/>
</RelativeLayout>
</RelativeLayout>
Here is the toolbar_ftp.xml file
<android.support.v7.widget.Toolbar 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/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Light">
<ImageView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:scaleType="centerInside"
android:src="@drawable/ftp_home_logo"
tools:ignore="ContentDescription" />
</android.support.v7.widget.Toolbar>
Here is the toolbar code
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
Upvotes: 2
Views: 1111
Reputation: 131
this may be silly but error also happens when setContentView
line accidentally removed!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
Upvotes: 0
Reputation: 1917
I tried all the solutions posted here, Still, I was experiencing the crash. Later I figured out the problem, It was because of giving an additional id in the include statement of the xml view.
<include layout="@layout/toolbar_ftp"
android:id="@+id/include" />
When referencing the toolbar, it became ambiguous for id inside id.
Upvotes: 2
Reputation: 849
if you are using AppcompatActivity try this :
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if you are using ActionBarActivity then try this :
this.getActionBar().setDisplayHomeAsUpEnabled(true);
Upvotes: 0
Reputation:
hi try to change theme style of your app
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorPrimary</item>
<item name="android:textColor">@color/black</item>
<item name="android:actionOverflowButtonStyle">@style/ActionButtonOverflow</item>
</style>
Upvotes: 2
Reputation: 1159
In most of the cases this problem can be caused by your theme. Check it again, and make sure that it's parent with Theme.AppCompat.Light.DarkActionBar.
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">true</item>
...
</style>
If your activity extends AppCompatActivity or ActionBarActivity, then you must call getSupportActionBar().
Upvotes: 0