Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setSupportActionBar() throws Nullpointer exception

Tags:

android

I am new to android and i was following following tutorial for Material Design Toolbar :

http://www.android4devs.com/2014/12/how-to-make-material-design-app.html

But after Implementation of the code. following error was shown in logcat :

04-01 19:16:10.214    2246-2253/com.example.bhaskar.ddit_results E/art﹕ Failed sending reply to debugger: Broken pipe
04-01 19:16:11.985    2246-2246/com.example.bhaskar.ddit_results E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.bhaskar.ddit_results, PID: 2246
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bhaskar.ddit_results/com.example.bhaskar.ddit_results.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
        at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:95)
        at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:88)
        at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:84)
        at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionBarActivityDelegateBase.java:175)
        at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.java:92)
        at com.example.bhaskar.ddit_results.MainActivity.onCreate(MainActivity.java:41)
        at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)   
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)    
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

which is caused by NullPointerException.I am using Android Studio 1.0.1.

styles.xml :

<resources>

<!-- Base application theme. -->
<style name="MyTheme" parent="MyTheme.Base">

</style>

<style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="colorPrimary">#ff32cd32</item>
    <item name="colorPrimaryDark">#ff04cd2c</item>
    <item name="colorAccent">#ff4081</item>
</style>

v21/styles.xml:

<resources>
<style name="MyTheme" parent="MyTheme.Base">
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>

In toolbar.xml i've :

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>

also i've included the toolbar layout in activity_main.xml file. In MainActivity.java :

public class MainActivity extends ActionBarActivity {

....



private Toolbar toolbar;
public static final int RECORD_ENTRY_REQUEST_CODE=3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set our view from the "main" layout resource

     toolbar = (Toolbar)findViewById(R.id.toolbar);

    //Toolbar will now take on default actionbar characteristics
    setContentView(R.layout.activity_main);
    setSupportActionBar(toolbar);
    setTitle("DDIT_Results");
    ....

}

I searched for this problem and didn't come up with any solutions.Hopefully, someone recognizes this error and has a quick answer for it - otherwise I can provide more information if requested.Any help would be appriciated.

like image 732
Prabhakar Avatar asked Apr 01 '15 14:04

Prabhakar


2 Answers

You need to call setContentView BEFORE adding any Views such as the Toolbar.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);         // <-- call this first

    toolbar = (Toolbar) findViewById(R.id.toolbar); // <-- setup views after

    setSupportActionBar(toolbar);
    setTitle("DDIT_Results");
}

Please see the Official Android Developers blog here: http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html

Action Bar

To use Toolbar as an Action Bar, first disable the decor-provided Action Bar. The easiest way is to have your theme extend from Theme.AppCompat.NoActionBar (or its light variant).

Second, create a Toolbar instance, usually via your layout XML:

<android.support.v7.widget.Toolbar
    android:id=”@+id/my_awesome_toolbar”
    android:layout_height=”wrap_content”
    android:layout_width=”match_parent”
    android:minHeight=”?attr/actionBarSize”
    android:background=”?attr/colorPrimary” />

The height, width, background, and so on are totally up to you; these are just good examples. As Toolbar is just a ViewGroup, you can style and position it however you want.

Then in your Activity or Fragment, set the Toolbar to act as your Action Bar:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.blah);

    final Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);
}

From this point on, all menu items are displayed in your Toolbar, populated via the standard options menu callbacks.

like image 81
Jared Burrows Avatar answered Oct 30 '22 19:10

Jared Burrows


Before adding toolbar you need to remove action from your style like

 <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>

Now define toolbar in res/xml/toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
   <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/appbar"
    android:theme="@style/AppTheme"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize" />

Now include this toolbar in our activity layout xml like

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <include
        android:id="@+id/tool_bar"
        layout="@xml/toolbar" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

Note : Now while defining toolbar in activity we will use "tool_bar" as id not "appbar" id

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar actionbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(actionbar);
    }
}

Now you can see in your activity toolbar

like image 42
Deepak Gupta Avatar answered Oct 30 '22 19:10

Deepak Gupta