ActionBar 抛出 NullPointerException

发布于 2024-12-20 21:31:31 字数 3283 浏览 1 评论 0原文

我无法弄清楚这个。我正在自定义我的 ActionBar(是的,我的应用程序适用于 3.0+)该代码在设备上运行良好(使用 Motorola xoom,如果重要的话)。但是当我在模拟器上运行它时,它会抛出 NPE。这是我的 ActionBar

/**
 * Customizes Action bar sets background color and assigns a layout to it
 */
private void customActionBar() {
    Log.v(TAG, "customizing ActionBar Entry");
    ActionBar actionBar = getActionBar();
    Log.v(TAG, "customizing ActionBar : "+actionBar.toString());
            //This is where i get NPE
    actionBar.setBackgroundDrawable(new ColorDrawable(Color
            .parseColor(Constants.COLOR)));
    Log.v(TAG, "customizing ActionBar -Background color : ");

    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.custom_action, null);
    // lay.setLayoutParams(new ActionBar.LayoutParams(
    // android.app.ActionBar.LayoutParams.MATCH_PARENT,
    // android.app.ActionBar.LayoutParams.MATCH_PARENT));
    actionBar.setCustomView(view);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    Log.v(TAG, "customizing ActionBar Exit");
}

编辑代码: logcat

V/>>> FullPdfViewerActivity(438): customizing ActionBar Entry
D/AndroidRuntime(438): Shutting down VM
W/dalvikvm(438): threadid=1: thread exiting with uncaught exception (group=0x40014760)
E/AndroidRuntime(438): FATAL EXCEPTION: main
E/AndroidRuntime(438): java.lang.RuntimeException: Unable to start activity                                 
ComponentInfo{com.test.android.tester/com.test.android.tester.core.AwesomePagerActivity}:          java.lang.NullPointerException
E/AndroidRuntime(438):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
E/AndroidRuntime(438):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
E/AndroidRuntime(438):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
E/AndroidRuntime(438):  at    android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
E/AndroidRuntime(438):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(438):  at android.os.Looper.loop(Looper.java:126)
E/AndroidRuntime(438):  at android.app.ActivityThread.main(ActivityThread.java:3997)
E/AndroidRuntime(438):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(438):  at java.lang.reflect.Method.invoke(Method.java:491)
E/AndroidRuntime(438):  at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
E/AndroidRuntime(438):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
E/AndroidRuntime(438):  at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(438): Caused by: java.lang.NullPointerException
E/AndroidRuntime(438):  at com.test.android.tester.core.AwesomePagerActivity.customActionBar(AwesomePagerActivity.java:227)
E/AndroidRuntime(438):  at com.test.android.tester.core.AwesomePagerActivity.onCreate(AwesomePagerActivity.java:187)
E/AndroidRuntime(438):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
E/AndroidRuntime(438):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
E/AndroidRuntime(438):  ... 11 more

我从我的 onCreate() 调用这个。我担心它是否适用于其他设备?

TIA

I couldnt figure out this one. I am customizing my ActionBar (and yes my App is for 3.0+) The code runs fine on Device(using Motorola xoom ,if it matters) .But when i run this on Emulator it throws NPE.This is my code for ActionBar

/**
 * Customizes Action bar sets background color and assigns a layout to it
 */
private void customActionBar() {
    Log.v(TAG, "customizing ActionBar Entry");
    ActionBar actionBar = getActionBar();
    Log.v(TAG, "customizing ActionBar : "+actionBar.toString());
            //This is where i get NPE
    actionBar.setBackgroundDrawable(new ColorDrawable(Color
            .parseColor(Constants.COLOR)));
    Log.v(TAG, "customizing ActionBar -Background color : ");

    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.custom_action, null);
    // lay.setLayoutParams(new ActionBar.LayoutParams(
    // android.app.ActionBar.LayoutParams.MATCH_PARENT,
    // android.app.ActionBar.LayoutParams.MATCH_PARENT));
    actionBar.setCustomView(view);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    Log.v(TAG, "customizing ActionBar Exit");
}

EDIT : logcat

V/>>> FullPdfViewerActivity(438): customizing ActionBar Entry
D/AndroidRuntime(438): Shutting down VM
W/dalvikvm(438): threadid=1: thread exiting with uncaught exception (group=0x40014760)
E/AndroidRuntime(438): FATAL EXCEPTION: main
E/AndroidRuntime(438): java.lang.RuntimeException: Unable to start activity                                 
ComponentInfo{com.test.android.tester/com.test.android.tester.core.AwesomePagerActivity}:          java.lang.NullPointerException
E/AndroidRuntime(438):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
E/AndroidRuntime(438):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
E/AndroidRuntime(438):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
E/AndroidRuntime(438):  at    android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
E/AndroidRuntime(438):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(438):  at android.os.Looper.loop(Looper.java:126)
E/AndroidRuntime(438):  at android.app.ActivityThread.main(ActivityThread.java:3997)
E/AndroidRuntime(438):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(438):  at java.lang.reflect.Method.invoke(Method.java:491)
E/AndroidRuntime(438):  at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
E/AndroidRuntime(438):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
E/AndroidRuntime(438):  at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(438): Caused by: java.lang.NullPointerException
E/AndroidRuntime(438):  at com.test.android.tester.core.AwesomePagerActivity.customActionBar(AwesomePagerActivity.java:227)
E/AndroidRuntime(438):  at com.test.android.tester.core.AwesomePagerActivity.onCreate(AwesomePagerActivity.java:187)
E/AndroidRuntime(438):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
E/AndroidRuntime(438):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
E/AndroidRuntime(438):  ... 11 more

I call this from my onCreate() .I am worried if it will work for other devices??

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤寂小茶 2024-12-27 21:31:31

我认为,getActionBar() 返回 null。当在 Activity 的 setContentView() 之前调用 getActionBar() 时,我看到了这种行为,但仅在 Android 3.0 和 3.1 上,在 Android 3.2 上顺序并不重要。
尝试在自定义操作栏之前调用 setContentView

I think, that getActionBar() returns null. I saw this behavior when getActionBar() was called before setContentView() of activity, but only on Android 3.0 and 3.1, on Android 3.2 order does not matter.
Try to call setContentView before customizing action bar

晨曦慕雪 2024-12-27 21:31:31

请记住,在某些情况下,您可能需要在 onActivityCreated 中执行任何调用,因为视图可能尚未准备好。

Keep in mind that in some circumstances, you may need to do any calls in onActivityCreated as the view may not be ready.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文