【发布时间】:2014-06-04 10:51:36
【问题描述】:
我是新手,所以放轻松:)
我正在尝试使用 java 编辑相对布局的 android:background 属性,但我不断收到空指针错误。下面代码中的可绘制对象是包含在单个 .xml 文件中的渐变。当我通过drawer_list_item.xml 文件添加选择器时,选择器没有任何问题。
这是我的代码:
MainActivity.java
public void DrawerListItem(){
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.test);
Resources res = getResources();
Drawable listSelector = res.getDrawable(R.drawable.list_selector);
relativeLayout.setBackgroundDrawable(listSelector);
}
drawer_list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/list_selector_dark"
android:id="@+id/test">
</RelativeLayout>
list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>
</selector>
list_selector_dark.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/test_normal_dark" android:state_activated="false"/>
<item android:drawable="@drawable/test_pressed_dark" android:state_pressed="true"/>
<item android:drawable="@drawable/test_pressed_dark" android:state_activated="true"/>
</selector>
LogCat:
06-04 20:34:10.596: E/AndroidRuntime(12209): FATAL EXCEPTION: main
06-04 20:34:10.596: E/AndroidRuntime(12209): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.orgbiztech.biznotes/com.orgbiztech.biznotes.MainActivity}: java.lang.NullPointerException
06-04 20:34:10.596: E/AndroidRuntime(12209): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
06-04 20:34:10.596: E/AndroidRuntime(12209): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
06-04 20:34:10.596: E/AndroidRuntime(12209): at android.app.ActivityThread.access$700(ActivityThread.java:159)
06-04 20:34:10.596: E/AndroidRuntime(12209): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
06-04 20:34:10.596: E/AndroidRuntime(12209): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 20:34:10.596: E/AndroidRuntime(12209): at android.os.Looper.loop(Looper.java:137)
06-04 20:34:10.596: E/AndroidRuntime(12209): at android.app.ActivityThread.main(ActivityThread.java:5419)
06-04 20:34:10.596: E/AndroidRuntime(12209): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 20:34:10.596: E/AndroidRuntime(12209): at java.lang.reflect.Method.invoke(Method.java:525)
06-04 20:34:10.596: E/AndroidRuntime(12209): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
06-04 20:34:10.596: E/AndroidRuntime(12209): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
06-04 20:34:10.596: E/AndroidRuntime(12209): at dalvik.system.NativeStart.main(Native Method)
06-04 20:34:10.596: E/AndroidRuntime(12209): Caused by: java.lang.NullPointerException
06-04 20:34:10.596: E/AndroidRuntime(12209): at com.orgbiztech.biznotes.MainActivity.DrawerListItem(MainActivity.java:234)
06-04 20:34:10.596: E/AndroidRuntime(12209): at com.orgbiztech.biznotes.MainActivity.onCreate(MainActivity.java:65)
06-04 20:34:10.596: E/AndroidRuntime(12209): at android.app.Activity.performCreate(Activity.java:5372)
06-04 20:34:10.596: E/AndroidRuntime(12209): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
06-04 20:34:10.596: E/AndroidRuntime(12209): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
06-04 20:34:10.596: E/AndroidRuntime(12209): ... 11 more
【问题讨论】:
-
第 234 行是哪一行?
-
看起来您的
getResources()正在返回NULL -
getResources 不返回 null。 drawer_list_item.xml 是您作为 setContentView 参数提供的布局?
-
你需要这样的资源上下文 context.getRessource();
-
这样试试 'public void DrawerListItem(){ RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.test);相对布局 .setBackgroundResource(R.drawable.blue_gradient_background); }'
标签: java android xml android-relativelayout