6

我正在尝试将我的自定义视图添加到操作栏中。

final View addView=getLayoutInflater().inflate(R.layout.home_actionbar_buttons, null);
        getActionBar().setCustomView(addView);

我的 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

但我得到空指针异常。当我调试时,addView 对象被填充并且它不为空。膨胀时 ViewGroupRoot 是强制性的吗?

编辑 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@app@com.example.actionbar-1.apk@classes.dex -> /data/dalvik-cache/data@app@com.example.actionbar-1.apk@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

编辑2:

setCustomView(R.layout.mylayout.xml);在将 customView 添加到操作栏之前添加

仍然无法在操作栏中看到按钮

4

1 回答 1

22

我认为您的问题很可能是由于您的活动中未包含操作栏而导致 getActionBar() 调用失败。尝试添加以下属性:

android:targetSdkVersion="11"

到清单中的“uses-sdk”节点。通过定位一个蜂窝 SDK,您将获得一个包含在您的活动中的操作栏。您还可以通过将应用程序的主题设置为 Holo 主题之一来实现此目的。例如

android:theme="@android:style/Theme.Holo.Light"

如果您在清单的应用程序节点中包含该属性,您还应该获得一个可以使用的操作栏。

您还需要将操作栏上的显示选项设置为 DISPLAY_SHOW_CUSTOM 以显示任何自定义视图。设置自定义视图后尝试添加此行:

getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

在“使用操作栏”下查看此页面以获取更多详细信息:http: //developer.android.com/reference/android/app/ActionBar.html

希望有帮助!

于 2011-09-30T07:35:52.047 回答