2010-03-10 10 views
10

Tengo un código que funciona el 99% del tiempo debido a que se despliegue en una gran cantidad de clientes, pero a veces me sale el siguiente:InvocationTargetException en inflar un xml - androide

java.lang.reflect.InvocationTargetException android.widget.LinearLayout. (LinearLayout.java:92) java.lang.reflect.Constructor.constructNative (Método nativo) 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) .....

y en mi código que tengo: li

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

así que la pregunta es por qué estoy obteniendo InvocationTargetException.

Gracias

Respuesta

15

Puede intentar getLayoutInflater() en lugar de su llamada getSystemService(), aunque no estoy seguro de que hará una diferencia.

Un InvocationTargetException viene de la reflexión, y significa que el Method que se invocó arrojó un Exception. ¿Ve algún signo de otro rastro de pila que podría ser el Exception subyacente? Si no, intente capturar InvocationTargetException y mirar getCause() para ver qué está pasando realmente.

+0

gracias, voy a tratar para llegar a la causa de eso. –

8

También tuve el mismo problema.

que se resolvió el problema:

Hacer la variable local

private Context **context**; 

Luego, en el constructor de la clase (que ha contexto argumento Contexto) hacer esto

this.context=**context**; 

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

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

Gracias, funciona perfectamente, pero no sé por qué @@ – VAdaihiep

+1

Es porque 'getContext()' puede dar como resultado un contexto diferente (envuelto) que el que se pasó en el constructor – Takhion

Cuestiones relacionadas