gpt4 book ai didi

android - ImageView 上的 setPadding 崩溃 Activity

转载 作者:行者123 更新时间:2023-11-29 21:52:29 27 4
gpt4 key购买 nike

我使用 actionbarcompat 在 pre honeycomp 设备上也有一个 actionbar。

由于主页图标和操作栏标题之间​​没有空格,我将其添加到我的 Activity 中:

// add padding to the right of home item
ImageView view = (ImageView)findViewById(android.R.id.home);
view.setPadding(0, 0, 10, 0);

实际上是第二行 (setPadding) 导致了崩溃。

它在 Jelly Bean 设备上运行良好。但是我得到了一个空指针异常,例如运行它Android 2.3 设备。

这是崩溃的 logcat 输出:

12-29 11:44:36.968: E/AndroidRuntime(329): FATAL EXCEPTION: main
12-29 11:44:36.968: E/AndroidRuntime(329): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.GetRichOrDieSmoking/com.GetRichOrDieSmoking.MainScreen}: java.lang.NullPointerException
12-29 11:44:36.968: E/AndroidRuntime(329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-29 11:44:36.968: E/AndroidRuntime(329): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-29 11:44:36.968: E/AndroidRuntime(329): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-29 11:44:36.968: E/AndroidRuntime(329): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-29 11:44:36.968: E/AndroidRuntime(329): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 11:44:36.968: E/AndroidRuntime(329): at android.os.Looper.loop(Looper.java:123)
12-29 11:44:36.968: E/AndroidRuntime(329): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-29 11:44:36.968: E/AndroidRuntime(329): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 11:44:36.968: E/AndroidRuntime(329): at java.lang.reflect.Method.invoke(Method.java:507)
12-29 11:44:36.968: E/AndroidRuntime(329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-29 11:44:36.968: E/AndroidRuntime(329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-29 11:44:36.968: E/AndroidRuntime(329): at dalvik.system.NativeStart.main(Native Method)
12-29 11:44:36.968: E/AndroidRuntime(329): Caused by: java.lang.NullPointerException
12-29 11:44:36.968: E/AndroidRuntime(329): at com.GetRichOrDieSmoking.MainScreen.onCreate(MainScreen.java:463)
12-29 11:44:36.968: E/AndroidRuntime(329): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-29 11:44:36.968: E/AndroidRuntime(329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-29 11:44:36.968: E/AndroidRuntime(329): ... 11 more

最佳答案

android.R.id.home仅在 API 级别 11 中引入。因此无法在 Android 2.3 中创建 View 。因此在下一行中,当您尝试在 null View 上设置填充时,您会得到 NullPointerException

关于android - ImageView 上的 setPadding 崩溃 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14081378/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com