膨胀 xml 时的 InvocationTargetException - android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2417341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
InvocationTargetException on inflating an xml - android
提问by Daniel Benedykt
I have a code that works 99% of the time since is deploy in lots of clients, but sometimes I get the following:
我有一个代码可以在 99% 的时间内工作,因为它被部署在很多客户端中,但有时我会得到以下信息:
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) .....
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);
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.
所以问题是为什么我会收到 InvocationTargetException。
Thanks
谢谢
回答by CommonsWare
You can try getLayoutInflater()instead of your getSystemService()call, though I am not sure that will make a difference.
您可以尝试getLayoutInflater()代替您的getSystemService()电话,但我不确定这会有所作为。
An InvocationTargetExceptioncomes from reflection, and means the Methodthat was invoked threw an Exception. Do you see any sign of another stack trace that might be the underlying Exception? If not, try catching InvocationTargetExceptionand looking at getCause()to see what is really going on.
AnInvocationTargetException来自反射,意味着Method被调用的 抛出了一个Exception。您是否看到可能是底层的另一个堆栈跟踪的任何迹象Exception?如果没有,请尝试捕捉InvocationTargetException并查看getCause()实际情况。
回答by PritiJinny
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);

