0

I'm new to this so go easy :)

I'm trying edit the android:background property of a relative layout using java but I keep getting a Null Pointer Error. The drawables in the code below are gradients contained within individual .xml files. The selectors don't have any issue when I add them via the drawer_list_item.xml file.

Here's my code:

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>

The 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
Adam Arold
  • 29,285
  • 22
  • 112
  • 207
Frazer
  • 1

1 Answers1

0

I don't see your line numbers but it feels like "relativeLayout" is null when you call

relativeLayout.setBackgroundDrawable(listSelector);

That would mean that you probably call DrawerListItem() too early and your relative view hasn't been inflated yet.

Try to put a breakpoint to that line and check the value of relativeLayout for null or you can also just log that value before executing the line. If RelativeLayout is really null, make sure to call your DrawerListItem() only after calling setContentView() in your onCreate().

If that doesn't help, show us your whole onCreate() or search for "findViewById returns null" here on SO (see e.g. findViewByID returns null).

By the way, method names should start with lowercase letter by convention in Java. So consider renaming DrawerListItem() to drawerListItem()

Community
  • 1
  • 1
tomorrow
  • 1,260
  • 1
  • 14
  • 26