关于膨胀xml的InvocationTargetException - android

Dan*_*ykt 10 xml android inflate

我有一个代码在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 ).....

在我的代码上我有:

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

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

谢谢

Com*_*are 17

您可以尝试getLayoutInflater()而不是getSystemService()通话,但我不确定这会有所作为.

一个InvocationTargetException来自反射,意味着Method被调用的东西扔了一个Exception.您是否看到任何可能是底层的堆栈跟踪的迹象Exception?如果没有,请尝试捕捉InvocationTargetException并查看getCause()真实情况.


小智 9

我也有同样的问题.

我解决了这个问题:

制作局部变量

private Context **context**;
Run Code Online (Sandbox Code Playgroud)

然后在您的类构造函数(具有参数Context上下文)中执行此操作

this.context=**context**;

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

theview = li.inflate(R.layout.partofthescreen, somecontainer, false);
Run Code Online (Sandbox Code Playgroud)