【问题标题】:InvocationTargetException on inflating an xml - androidInvocationTargetException on inflating an x​​ml - android
【发布时间】:2011-01-25 22:00:47
【问题描述】:

我有一个代码在 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, 某个容器,假);

所以问题是我为什么会收到 InvocationTargetException。

谢谢

【问题讨论】:

    标签: xml android inflate


    【解决方案1】:

    我也遇到了同样的问题。

    我通过以下方式解决了这个问题:

    制作局部变量

    private Context **context**;
    

    然后在你的类构造函数(有参数上下文上下文)中这样做

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

    【讨论】:

    • 这是因为getContext() 可能会导致与构造函数中传递的上下文不同(包装)
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      • 2020-07-24
      • 2022-01-12
      相关资源
      最近更新 更多