InvocationTargeTexception para inflar um XML - Android
Pergunta
Eu tenho um código que funciona 99% das vezes desde que é implantado em muitos clientes, mas às vezes recebo o seguinte:
java.lang.reflect.invocationTargeTexception Android.widget.linearlayout. (linearlayout.java:92) java.lang.reflect.constructor.Constructnative (nativo) (método nativo) java.Lang.reflect.constructor.NewInStance (construção) (nativo) .view.layoutInflater.createview (layoutInflater.java:499) com.android.internal.policy.impl.phonelayoutinflater.oCreateView (phoneLayoutinflater.java:56) Android.View.Layoutinflater.creatEviewtInkoutwloutwr) .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 ) .....
E no meu código que tenho:
LayoutInflater li = (layoutInflater) Este .getSystemService (context.layout_inflater_service);
theview = li.inflate (r.layout.partofthescreen, somecontainer, false);
Portanto, a questão é por que estou recebendo InvocationTargeTexception.
Obrigado
Solução
Podes tentar getLayoutInflater()
em vez de você getSystemService()
Ligue, embora eu não tenha certeza de que isso fará a diferença.
Um InvocationTargetException
vem da reflexão e significa o Method
que foi invocado jogou um Exception
. Você vê qualquer sinal de outro rastreamento de pilha que possa ser o subjacente Exception
? Se não, tente pegar InvocationTargetException
e olhando para getCause()
Para ver o que realmente está acontecendo.
Outras dicas
Eu também tive o mesmo problema.
Eu resolvi esse problema por:
Faça a variável local
private Context **context**;
Então, no seu construtor de classe (que tem contexto de argumento), faça isso
this.context=**context**;
LayoutInflater li = (LayoutInflater) **context** .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
theview = li.inflate(R.layout.partofthescreen, somecontainer, false);