Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException when inflating , inside the graphical designer

I'm creating a custom view that extends from FrameLayout.

in each of the CTORs , i'm using a LayoutInflater to inflate some views from XML , using something like this (in a function called "init()" ) :

final LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.custom_view, this, true); // <= here there is an NPE 

the problem is, even though the app works fine with this code, the graphical designer constantly fails to render the custom view- it keeps writing something like this in the error log:

The following classes could not be instantiated:
- com.example.customviewtest.CustomView (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.
Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse

android.content.res.Resources$NotFoundException: Could not resolve resource value: 0x7F030018.
    at android.content.res.BridgeResources.throwException(BridgeResources.java:693)
    at android.content.res.BridgeResources.getLayout(BridgeResources.java:271)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
    at com.example.customviewtest.CustomView.init(CustomView .java:38)
    at com.example.customviewtest.CustomView.<init>(CustomView .java:26)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(    at sun.reflect.NativeConstructorAccessorImpl.newInstance(    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(    at java.lang.reflect.Constructor.newInstance(    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:422)
    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:179)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:746)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:372)

it seems to occur only sometimes, and i don't understand on which cases. it's like the real code is not in sync with the graphical editor. sometimes it even claims i'm trying to cast a view to a totally different type of view.

my question

why does it occur? is it an ADT bug?

i've tried multiple ways to use the inflater, including :

getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)

and i also tried to do the inflating on the onFinishInflate method, but nothing worked.

it seems that many have posted about this problem, but none found a solution and simply used isInEditMode for disabling the inflating in the first place.

like image 912
android developer Avatar asked Apr 24 '26 16:04

android developer


1 Answers

Go to your R Class file and find what points to 0x7F030018

Edit

You must add

if (!isInEditMode())
{
   // Your major LayoutInflater codes goes here
}

in your custom view's constructor. Then it'll build.

like image 78
Naresh Avatar answered Apr 27 '26 09:04

Naresh