2010-03-10 149 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