2

i get null pointer error in line :

rl.setBackgroundResource(R.drawable.headerbackground);

MainActivity code:

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View mainLayout = inflater.inflate(R.layout.main_layout, null, false);
View includedHeader = mainLayout.findViewById(R.id.header);
RelativeLayout rl = (RelativeLayout) includedHeader.findViewById(R.id.header_root);
rl.setBackgroundResource(R.drawable.headerbackground);
setContentView(mainLayout);

in main_layout.xml I include header_layout :

<include android:id="@+id/header" layout="@layout/header_layout"/>

and in header_layout.xml:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/header_root" />

question: why rl is null and how access to it in my code?

EDITED: logcat logs(mainActivity.java:58 is rl.setBackground(R.drawable.headerbackround) in my code):

Caused by: java.lang.NullPointerException
        at com.npi.blureffect.MainActivity.onCreate(MainActivity.java:58)
        at android.app.Activity.performCreate(Activity.java:5243)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)

   

Cœur
  • 37,241
  • 25
  • 195
  • 267
mk72
  • 1,307
  • 2
  • 8
  • 15
  • post ur log and full code pls.. – M S Gadag Dec 23 '14 at 08:50
  • what is line no 58 in `MainActivity.java`? – SilentKiller Dec 23 '14 at 09:13
  • @SilentKiller: rl.setBackgroundResource(R.drawable.headerbackground); – mk72 Dec 23 '14 at 09:16
  • That indicates `rl` is null even after `RelativeLayout rl = (RelativeLayout) includedHeader.findViewById(R.id.header_root);`. Are you sure you have layout `header_root` in xml `header`. If yes, Clean build and check – MysticMagicϡ Dec 23 '14 at 12:10
  • @MagicalPhoenixϡ : yes im sure i check it again. if i retrive other element of header_layout.xml program run without error but only for access to Relative layout genereate error. why?? – mk72 Dec 23 '14 at 12:50

4 Answers4

0

You are setting your ContentView in the wrong order. You have to first set the ContentView, then change the background color :

setContentView(R.layout.main_layout);
// the include tag is included dynamically in the root view
RelativeLayout rl = (RelativeLayout) this.findViewById(R.id.header_root);
rl.setBackgroundResource(R.drawable.headerbackground);
pdegand59
  • 12,776
  • 8
  • 51
  • 58
  • first time i coded like this but return null pointer. rl is in lncluded layout and Unfortunately this not work – mk72 Dec 23 '14 at 08:55
0

Instead of fetching it from includedHeader try to fetch it directly from mainLayout. You don't need to assign id to include tag and bind it in java. You can directly fetch views

Replace

RelativeLayout rl = (RelativeLayout) includedHeader.findViewById(R.id.header_root);

with

RelativeLayout rl = (RelativeLayout) mainLayout.findViewById(R.id.header_root);

Your code will be

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View mainLayout = inflater.inflate(R.layout.main_layout, null, false);
RelativeLayout rl = (RelativeLayout) mainLayout.findViewById(R.id.header_root);
rl.setBackgroundResource(R.drawable.headerbackground);
setContentView(mainLayout);
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
0

You should try debugging and see at line rl.setBackgroundResource(R.drawable.headerbackground); if the variable rl is null. In case it's not then maybe the drawable is null.

EDIT:

rl is null because includedHeader must be null. Try removing the line View includedHeader = mainLayout.findViewById(R.id.header); and replace with RelativeLayout rl = (RelativeLayout) mainLayout.findViewById(R.id.header_root); Afterwards check to see if mainLayout or rl are null (while debugging)

EDIT 2:

When you are creating reusable layouts (ones that will be later included using include) it's always best to make the root a FrameLayout

Also check this page out and maybe you can use the <merge> tag instead.

Aleks Nine
  • 249
  • 7
  • 15
  • i check it. rl is null – mk72 Dec 23 '14 at 11:06
  • yes. i find out when i attempt to retrive other element(view) of header_layout.xml everything is ok but only when i want access to root view of header_layout.xml app stopped with null pointer error – mk72 Dec 23 '14 at 14:47
  • check to see if you have some ids appearing multiple times in the app. In other words try to change `header_root` to `header_root_new` for example. – Aleks Nine Dec 23 '14 at 14:53
  • no . i cheked it. if i add a viewGroup like relativeLayout to top of the hader_layout and make hader_root a child of it error will gone but why???? why root view of included layout is null? – mk72 Dec 23 '14 at 15:02
0

Some code like your works very well for me in Android 4.2.2 until nowadays.

But, I don't know why, after Android 4.4 a (RelativeLayout) included dynamically inside another (RelativeLayout) returns null after a rootLayout.findViewById( ... ).

In this cases, you can safely find by views using root layout directly!

So, to correct work this code you must include only one line of code:

LayoutInflater inflater = (LayoutInflater) 
getSystemService(LAYOUT_INFLATER_SERVICE);
View mainLayout = inflater.inflate(R.layout.main_layout, null, false);
View includedHeader = mainLayout.findViewById(R.id.header);
RelativeLayout rl = (RelativeLayout) includedHeader.findViewById(R.id.header_root);

if ( rl == null ) { rl = includedHeader; } // include this line

rl.setBackgroundResource(R.drawable.headerbackground);
setContentView(mainLayout);

This code works succesfully for all Androids before and after 4.4

Berst regards!

alijunior
  • 317
  • 1
  • 12