I am trying to add my custom view into Action bar.
final View addView=getLayoutInflater().inflate(R.layout.home_actionbar_buttons, null);
getActionBar().setCustomView(addView);
my XML,
<LinearLayout android:id="@+id/linearLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="match_parent">
<Button android:text="Refresh" android:layout_height="wrap_content"
android:id="@+id/btn_refresh" android:layout_width="wrap_content"></Button>
</LinearLayout
but i get nullpointer exception. when i debug,addView object gets populated and its not null. is ViewGroupRoot mandatory while inflating?
EDIT 1: Logcat
09-30 12:50:11.625: DEBUG/PackageManager(74): Activities: com.example.actionbar.MainActivity
09-30 12:50:11.645: INFO/ActivityManager(74): Force stopping package com.example.actionbar uid=10047
09-30 12:50:11.825: INFO/installd(34): move /data/dalvik-cache/data@[email protected]@classes.dex -> /data/dalvik-cache/data@[email protected]@classes.dex
09-30 12:50:11.825: DEBUG/PackageManager(74): New package installed in /data/app/com.example.actionbar-1.apk
09-30 12:50:12.215: DEBUG/PackageManager(74): generateServicesMap(android.accounts.AccountAuthenticator): 3 services unchanged
09-30 12:50:12.306: DEBUG/PackageManager(74): generateServicesMap(android.content.SyncAdapter): 5 services unchanged
09-30 12:50:12.475: WARN/RecognitionManagerService(74): no available voice recognition services found
09-30 12:50:12.605: DEBUG/dalvikvm(143): GC_EXPLICIT freed 2K, 17% free 14616K/17415K, paused 5ms+26ms
09-30 12:50:12.985: INFO/AccountTypeManager(202): Loaded meta-data for 1 account types, 0 accounts in 14ms
09-30 12:50:13.555: DEBUG/dalvikvm(143): GC_FOR_ALLOC freed 1125K, 17% free 14564K/17415K, paused 121ms
09-30 12:50:14.435: DEBUG/dalvikvm(143): GC_FOR_ALLOC freed 4109K, 33% free 11749K/17415K, paused 155ms
09-30 12:50:15.254: DEBUG/dalvikvm(143): GC_CONCURRENT freed 1055K, 29% free 12509K/17415K, paused 5ms+10ms
09-30 12:50:15.894: DEBUG/dalvikvm(143): GC_CONCURRENT freed 1109K, 24% free 13406K/17415K, paused 6ms+6ms
09-30 12:50:16.404: DEBUG/dalvikvm(143): GC_CONCURRENT freed 1084K, 18% free 14348K/17415K, paused 5ms+6ms
09-30 12:50:16.924: DEBUG/dalvikvm(74): GC_EXPLICIT freed 752K, 47% free 14474K/26951K, paused 49ms+21ms
09-30 12:50:16.944: DEBUG/AndroidRuntime(1196): Shutting down VM
09-30 12:50:16.994: INFO/AndroidRuntime(1196): NOTE: attach of thread 'Binder Thread #3' failed
09-30 12:50:17.044: DEBUG/dalvikvm(1196): GC_CONCURRENT freed 111K, 84% free 331K/2048K, paused 4ms+1ms
09-30 12:50:17.044: DEBUG/jdwp(1196): Got wake-up signal, bailing out of select
09-30 12:50:17.044: DEBUG/dalvikvm(1196): Debugger has detached; object registry had 1 entries
09-30 12:50:17.974: DEBUG/AndroidRuntime(1210): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
09-30 12:50:17.974: DEBUG/AndroidRuntime(1210): CheckJNI is ON
09-30 12:50:19.174: DEBUG/AndroidRuntime(1210): Calling main entry com.android.commands.am.Am
09-30 12:50:19.234: INFO/ActivityManager(74): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.actionbar/.MainActivity } from pid 1210
09-30 12:50:19.264: WARN/WindowManager(74): Failure taking screenshot for (216x135) to layer 21005
09-30 12:50:19.315: DEBUG/AndroidRuntime(1210): Shutting down VM
09-30 12:50:19.384: INFO/AndroidRuntime(1210): NOTE: attach of thread 'Binder Thread #3' failed
09-30 12:50:19.384: INFO/ActivityManager(74): Start proc com.example.actionbar for activity com.example.actionbar/.MainActivity: pid=1218 uid=10047 gids={}
09-30 12:50:19.484: DEBUG/dalvikvm(1210): GC_CONCURRENT freed 112K, 83% free 358K/2048K, paused 1ms+1ms
09-30 12:50:19.484: DEBUG/jdwp(1210): Got wake-up signal, bailing out of select
09-30 12:50:19.484: DEBUG/dalvikvm(1210): Debugger has detached; object registry had 1 entries
09-30 12:50:22.095: DEBUG/AndroidRuntime(1218): Shutting down VM
09-30 12:50:22.095: WARN/dalvikvm(1218): threadid=1: thread exiting with uncaught exception (group=0x40014760)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): FATAL EXCEPTION: main
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.actionbar/com.example.actionbar.MainActivity}: java.lang.NullPointerException
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): at android.os.Looper.loop(Looper.java:126)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): at android.app.ActivityThread.main(ActivityThread.java:3997)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): at java.lang.reflect.Method.invoke(Method.java:491)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): at dalvik.system.NativeStart.main(Native Method)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): Caused by: java.lang.NullPointerException
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): at com.example.actionbar.MainActivity.onCreate(MainActivity.java:16)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
09-30 12:50:22.134: ERROR/AndroidRuntime(1218): ... 11 more
09-30 12:50:24.615: DEBUG/dalvikvm(406): GC_EXPLICIT freed 8K, 6% free 6199K/6531K, paused 4ms+382ms
09-30 12:50:26.825: DEBUG/dalvikvm(143): GC_EXPLICIT freed 991K, 16% free 14644K/17415K, paused 977ms+245ms
09-30 12:50:28.545: WARN/ActivityManager(74): Force finishing activity com.example.actionbar/.MainActivity
09-30 12:50:28.545: WARN/WindowManager(74): Failure taking screenshot for (216x135) to layer 21010
09-30 12:50:29.178: WARN/ActivityManager(74): Activity pause timeout for ActivityRecord{40a10fe0 com.example.actionbar/.MainActivity}
09-30 12:50:29.287: WARN/ActivityManager(74): Launch timeout has expired, giving up wake lock!
09-30 12:50:30.925: WARN/ActivityManager(74): Activity idle timeout for ActivityRecord{40a10fe0 com.example.actionbar/.MainActivity}
09-30 12:50:33.144: INFO/Process(1218): Sending signal. PID: 1218 SIG: 9
09-30 12:50:33.344: INFO/ActivityManager(74): Process com.example.actionbar (pid 1218) has died.
09-30 12:50:33.554: WARN/InputManagerService(74): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@406a8790
09-30 12:50:33.874: INFO/Process(1179): Sending signal. PID: 1179 SIG: 9
09-30 12:50:36.494: DEBUG/dalvikvm(420): GC_EXPLICIT freed 10K, 6% free 6195K/6531K, paused 7ms+5ms
09-30 12:50:40.959: WARN/ActivityManager(74): Activity destroy timeout for ActivityRecord{40a10fe0 com.example.actionbar/.MainActivity}
09-30 12:50:41.595: DEBUG/dalvikvm(202): GC_EXPLICIT freed 174K, 5% free 6733K/7047K, paused 10ms+4ms
09-30 12:50:46.618: DEBUG/dalvikvm(345): GC_EXPLICIT freed 17K, 5% free 6277K/6599K, paused 7ms+9ms
EDIT 2:
Added setCustomView(R.layout.mylayout.xml);
before adding to customView into actionbar
still not able to see the button in action bar
I think your problem is most likely that the getActionBar() call is failing due to the action bar not being included in your activity. Try adding the following attribute:
android:targetSdkVersion="11"
to the 'uses-sdk' node in your manifest. By targeting a honeycomb SDK, you'll get an action bar included in your activity. You can also achieve this by setting the theme of your application to one of the Holo themes. e.g.
android:theme="@android:style/Theme.Holo.Light"
If you include that attribute in the application node of the manifest, you should also get an action bar to play with.
You also need to set the display options on the action bar to DISPLAY_SHOW_CUSTOM for any custom view to appear. Try adding this line after setting the custom view:
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Check out this page for under 'Using the Action Bar' for more detail: http://developer.android.com/reference/android/app/ActionBar.html
Hope that helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With