3

I want to add a toolbar to my app instead of Action bar. But when i add toolbar to my app, my app crashes and i don't know how.

This is my code.

ToolbarActivity.java

public class ToolbarActivity extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_toolbar); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarsdfs); if (toolbar != null) { setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } } } 

activity_toolbar.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <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/toolbarsdfs" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" android:background="?attr/colorPrimary" /> </LinearLayout> 

style.xml

<style name="AppTheme" parent="AppTheme.Base"/> <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/color1</item> <item name="colorPrimaryDark">@color/color2</item> <item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item> </style> 

LogCat

08-12 14:31:05.905: E/AndroidRuntime(18413): FATAL EXCEPTION: main 08-12 14:31:05.905: E/AndroidRuntime(18413): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tech.Toolbar/com.tech.toolbar.ToolbarActivity}: java.lang.NullPointerException 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1734) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1569) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:158) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:370) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:701) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.widget.TabHost.setCurrentTab(TabHost.java:373) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:167) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:487) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.view.View.performClick(View.java:2535) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.view.View$PerformClick.run(View.java:9129) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.os.Handler.handleCallback(Handler.java:618) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.os.Handler.dispatchMessage(Handler.java:123) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.os.Looper.loop(SourceFile:351) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.app.ActivityThread.main(ActivityThread.java:3820) 08-12 14:31:05.905: E/AndroidRuntime(18413): at java.lang.reflect.Method.invokeNative(Native Method) 08-12 14:31:05.905: E/AndroidRuntime(18413): at java.lang.reflect.Method.invoke(Method.java:538) 08-12 14:31:05.905: E/AndroidRuntime(18413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969) 08-12 14:31:05.905: E/AndroidRuntime(18413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727) 08-12 14:31:05.905: E/AndroidRuntime(18413): at dalvik.system.NativeStart.main(Native Method) 08-12 14:31:05.905: E/AndroidRuntime(18413): Caused by: java.lang.NullPointerException 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.support.v7.internal.app.WindowDecorActionBar.getDecorToolbar(WindowDecorActionBar.java:248) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.support.v7.internal.app.WindowDecorActionBar.init(WindowDecorActionBar.java:201) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.support.v7.internal.app.WindowDecorActionBar.<init>(WindowDecorActionBar.java:176) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.support.v7.app.AppCompatDelegateImplV7.createSupportActionBar(AppCompatDelegateImplV7.java:174) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.support.v7.app.AppCompatDelegateImplBase.getSupportActionBar(AppCompatDelegateImplBase.java:90) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.support.v7.app.AppCompatActivity.getSupportActionBar(AppCompatActivity.java:77) 08-12 14:31:05.905: E/AndroidRuntime(18413): at com.tech.toolbar.ToolBarActivity.onCreate(ToolBarctivity.java:12) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082) 08-12 14:31:05.905: E/AndroidRuntime(18413): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1698) 08-12 14:31:05.905: E/AndroidRuntime(18413): ... 18 more 08-12 14:31:05.920: E/dalvikvm(18413): [DVM] mmap return base = 45e92000 

Manifest

<uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/CustomActionBarTheme" > <activity android:name=".MainActivity" android:label="Tab" android:noHistory="true" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ToolBarActivity" android:theme="@style/AppTheme" /> </application> 

I have tried everything possible to achieve my goal but to no avail. My app min SDK is 10. I am using eclipse Luna for development.

11
  • 1
    why is toolbar inside a scrollview?. Also extend AppCompatActivity Commented Aug 12, 2015 at 9:24
  • How to extend AppCompatActivity and where to extend it? Commented Aug 12, 2015 at 9:27
  • 2
    Error is coming from the FeedbackActivity not from ToolbarActivity - FeedbackActivity.onCreate(FeedbackActivity.java:12) Commented Aug 12, 2015 at 9:39
  • 1
    Sorry but FeedbackActivity and ToolBarActivity is one and the same activity. Now i edited my question. Please answer now. @Ganesh Commented Aug 12, 2015 at 9:45
  • Sure that the error have to do with Toolbar? See logcat. There is one line with com.ansari.islamicthings.ToolBarActivity and another with com.tech.toolbar.ToolbarActivitywhich is now your activity and what is your package name?! Have you tried to start this activity without the toolbar? And what is line 12 in your activity? Commented Aug 14, 2015 at 11:50

8 Answers 8

1
+50

You are trying to find a view that is NULL.

Your code:

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

Your xml:

android:id="@+id/toolbarsdfs" 

ERROR!!

If you are trying to find your Toolbar, you've to put the ID of your Toolbar correctly!!

Your code of Toolbar should be:

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

By the way, change your "class extends" to this:

public class ToolbarActivity extends AppCompatActivity 

Hope it work :)

Edit

Okay. I've created a sample, tell me if I misunderstood your words, and I'll tell you what I've done.

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">#f0f0</item> <item name="colorPrimaryDark">#f3f</item> <item name="windowNoTitle">true</item> <item name="windowActionBar">false</item> </style> 

MainActivity.java

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarsdfs); if (toolbar != null) { setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } } 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbarsdfs" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" android:background="?attr/colorPrimaryDark" app:theme="@style/ThemeOverlay.AppCompat.ActionBar"> </android.support.v7.widget.Toolbar> 

And this is the result:

enter image description here

Is that what you want?

Sign up to request clarification or add additional context in comments.

3 Comments

Yes this is what i want bro.
Feel free to use my code, if you need more help message me ;) Happy coding ^^
Thanks brother its working. I will award my bounty to you in 22 hours.
0
public class ToolbarActivity extends AppCompatActivity 

this should be the declaration of your class, otherwise the theme with noActionBar and the class ActionBarActivity will create a conflict.

2 Comments

No, app is still crashing even after extending AppCompatActivity.
what does logcat say?
0

You've used <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar"> which is for no action bar screen. Try to use something like parent="@android:style/Widget.Holo.Light.ActionBar.Solid"

1 Comment

It's the other way around: "In order to use a Toolbar within the Activity's window content the application must not request the window feature FEATURE_ACTION_BAR." - .NoActionBar for ToolBar because stock (API 11) actionbar isn't as cool as custom compat one.
0

Remove your toolbar inside of ScrollView and add it outside of your ScrollView.

Try this snippet:

<LinearLayout 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" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <include android:id="@+id/toolbar" layout="@layout/toolbar" /> </LinearLayout> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="16sp" /> </LinearLayout> </ScrollView> </LinearLayout> 

In addition your activity should extend AppCompatActivity. ActionBarActivity is deprecated.

EDIT 1: Also you should add this line to your gradle

compile 'com.android.support:appcompat-v7:22.2.1' 

Maybe this help.

1 Comment

Nope it is still crashing.
0

Use them Theme.AppCompat.Light in place of Theme.AppCompat.Light.NoActionBar

<style name="AppTheme.Base" parent="Theme.AppCompat.Light"> <item name="colorPrimary">@color/color1</item> <item name="colorPrimaryDark">@color/color2</item> <item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item> </style> 

Comments

0

Try This:

mToolBar = (Toolbar) findViewById(Toolbar ID); setSupportActionBar(mToolBar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setTitle(resourses.getString(R.string.register)); 

If it requires:

/*mToolBar.setNavigationOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } });*/ 

Toolbar XML:

<?xml version="1.0" encoding="utf-8"?> <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" android:minHeight="?attr/actionBarSize" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

Note: I guess there is no need to extend Activity with AppCompatActivity, U have to add android-support-v7-appcompat lib in your project.

Comments

0

I have copied your code to a new project and it's working for me! My ToolbarActivity is (do you import android.support.v7.widget.Toolbar?):

package bendaf.example; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; public class ToolbarActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_toolbar); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarsdfs); if (toolbar != null) { setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } } } 

my Manifest file:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="bendaf.example" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme.Base" > <activity android:name=".ToolbarActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

my styles.xml (do you name it correctly?):

<resources> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> </style> <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@android:color/darker_gray</item> <item name="colorPrimaryDark">@android:color/holo_blue_dark</item> <item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item> </style> </resources> 

and my activity_toolbar.xml:

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

I hope you find the difference, my test phone runs android 5.0.1 and it's working on that.

Comments

0

in your style.xml,try this:-

 <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> </style> 

Hope it might help

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.