Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvocationTargetException on inflating an xml - android

I have a code that works 99% of the time since is deploy in lots of clients, but sometimes I get the following:

java.lang.reflect.InvocationTargetException android.widget.LinearLayout.(LinearLayout.java:92) java.lang.reflect.Constructor.constructNative(Native Method) java.lang.reflect.Constructor.newInstance(Constructor.java:446) android.view.LayoutInflater.createView(LayoutInflater.java:499) com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:562) android.view.LayoutInflater.rInflate(LayoutInflater.java:617) android.view.LayoutInflater.inflate(LayoutInflater.java:407) android.view.LayoutInflater.inflate(LayoutInflater.java:320) com.mycode.mycode.MyClass.draw(xxxxxxx) .....

and on my code I have:

LayoutInflater li = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
theview = li.inflate(R.layout.partofthescreen, somecontainer, false);

so the question is why I am getting InvocationTargetException.

Thanks

like image 736
Daniel Benedykt Avatar asked Mar 10 '10 13:03

Daniel Benedykt


2 Answers

You can try getLayoutInflater() instead of your getSystemService() call, though I am not sure that will make a difference.

An InvocationTargetException comes from reflection, and means the Method that was invoked threw an Exception. Do you see any sign of another stack trace that might be the underlying Exception? If not, try catching InvocationTargetException and looking at getCause() to see what is really going on.

like image 200
CommonsWare Avatar answered Oct 15 '22 05:10

CommonsWare


I also had the same problem.

I solved that problem by:

Make the local variable

private Context **context**;

Then in your class constructor( which has argument Context context) do this

this.context=**context**;

LayoutInflater li = (LayoutInflater) **context** .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

theview = li.inflate(R.layout.partofthescreen, somecontainer, false);
like image 22
PritiJinny Avatar answered Oct 15 '22 06:10

PritiJinny