这是我正在使用的代码:
progressFrame = (FrameLayout) findViewById(R.id.progressFrame);
progressFrame.setBackgroundResource(R.drawable.startprogress);
在Android 2.3.3上一切正常,但是在Android 4上尝试运行“ setBackgroundResource”行时崩溃。这是logcat消息:
07-06 17:31:28.159: W/dalvikvm(11848): threadid=1: thread exiting with uncaught exception (group=0x40be81f8)
07-06 17:31:28.167: E/AndroidRuntime(11848): FATAL EXCEPTION: main
07-06 17:31:28.167: E/AndroidRuntime(11848): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bosi.sweetsmell/com.bosi.sweetsmell.activity.RubActivity}: java.lang.NullPointerException
07-06 17:31:28.167: E/AndroidRuntime(11848): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
07-06 17:31:28.167: E/AndroidRuntime(11848): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-06 17:31:28.167: E/AndroidRuntime(11848): at android.app.ActivityThread.access$600(ActivityThread.java:128)
07-06 17:31:28.167: E/AndroidRuntime(11848): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-06 17:31:28.167: E/AndroidRuntime(11848): at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 17:31:28.167: E/AndroidRuntime(11848): at android.os.Looper.loop(Looper.java:137)
07-06 17:31:28.167: E/AndroidRuntime(11848): at android.app.ActivityThread.main(ActivityThread.java:4514)
07-06 17:31:28.167: E/AndroidRuntime(11848): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 17:31:28.167: E/AndroidRuntime(11848): at java.lang.reflect.Method.invoke(Method.java:511)
07-06 17:31:28.167: E/AndroidRuntime(11848): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-06 17:31:28.167: E/AndroidRuntime(11848): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-06 17:31:28.167: E/AndroidRuntime(11848): at dalvik.system.NativeStart.main(Native Method)
07-06 17:31:28.167: E/AndroidRuntime(11848): Caused by: java.lang.NullPointerException
07-06 17:31:28.167: E/AndroidRuntime(11848): at com.bosi.sweetsmell.activity.RubActivity.onCreate(RubActivity.java:35)
07-06 17:31:28.167: E/AndroidRuntime(11848): at android.app.Activity.performCreate(Activity.java:4465)
07-06 17:31:28.167: E/AndroidRuntime(11848): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
07-06 17:31:28.167: E/AndroidRuntime(11848): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-06 17:31:28.167: E/AndroidRuntime(11848): ... 11 more
希望有人可以提供帮助!
最佳答案
检查是否有多个合格的布局文件夹(例如layout-land,layout-hdpi,layout-xhdpi)。如果这样做,则可能是您的4.0仿真器是从单独的布局文件夹中提取的,在其中您可能没有为FrameLayout
正确分配ID。
关于java - 在Android 4上设置FrameLayout的背景时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11366209/