2010-03-10 146 views
10

我后来的作品中99%的时间在大量的客户之一是部署一个代码,但有时我得到如下:的InvocationTargetException在膨胀的XML - 机器人

java.lang.reflect.InvocationTargetException (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) .....

和我的代码有:

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

所以问题是为什么我得到InvocationTargetException。

感谢

回答

15

你可以试试getLayoutInflater(),而不是你getSystemService()电话,虽然我不知道会有所作为。

InvocationTargetException来自反思,意思是被调用的Method扔了Exception。你是否看到任何可能是底层Exception的堆栈跟踪迹象?如果没有,请尝试捕捉InvocationTargetException并查看getCause()以查看真正发生的情况。

+0

感谢,我会努力去找到它的原因。 – 2010-03-12 11:16:24

8

我也有同样的问题。

我解决了这个问题,通过:

使局部变量

private Context **context**; 

然后在类的构造函数(已参数上下文的背景下),这样做

this.context=**context**; 

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

theview = li.inflate(R.layout.partofthescreen, somecontainer, false); 
+0

谢谢,它完美的工作,但我不知道为什么@@ – VAdaihiep 2013-06-13 06:57:56

+1

这是因为'getContext()'可能会导致一个不同的(包装)上下文比在构造函数中传递的 – Takhion 2014-12-31 13:17:23