Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class LinearLayout 异常 #8

Closed
jp1017 opened this issue Nov 16, 2016 · 13 comments

Comments

@jp1017
Copy link

jp1017 commented Nov 16, 2016

MainActivity:

 public class MainActivity extends BaseActivity {
    @BindView(R.id.tv_hello)
    TextView mTextView;

    @Override
    protected void ComponentInject() {

    }

    @Override
    protected View initView() {
        return LayoutInflater.from(this).inflate(R.layout.activity_main, null, false);
    }

    @Override
    protected void initData() {

    }

}

出现异常:

Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class LinearLayout
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:3022)
at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3085)
at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:1678)
at butterknife.ButterKnife.bind(ButterKnife.java:123)
at com.inst.instcallcar.base.BaseActivity.onCreate(BaseActivity.java:91)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
at android.app.ActivityThread.access$800(ActivityThread.java:135) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5017) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException
at com.zhy.autolayout.config.AutoLayoutConifg.getMetaData(AutoLayoutConifg.java:98)
at com.zhy.autolayout.config.AutoLayoutConifg.init(AutoLayoutConifg.java:80)
at com.zhy.autolayout.utils.AutoLayoutHelper.initAutoLayoutConfig(AutoLayoutHelper.java:111)
at com.zhy.autolayout.utils.AutoLayoutHelper.(AutoLayoutHelper.java:103)
at com.zhy.autolayout.AutoLinearLayout.(AutoLinearLayout.java:18)
at com.inst.instcallcar.base.BaseActivity.onCreateView(BaseActivity.java:50)
at android.app.Activity.onCreateView(Activity.java:4744)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:36)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:77)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
at android.view.LayoutInflater.inflate(LayoutInflater.java:469) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:3022) 
at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3085) 
at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:1678) 
at butterknife.ButterKnife.bind(ButterKnife.java:123) 
at com.inst.instcallcar.base.BaseActivity.onCreate(BaseActivity.java:91) 
at android.app.Activity.performCreate(Activity.java:5231) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
at android.app.ActivityThread.access$800(ActivityThread.java:135) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5017) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
at dalvik.system.NativeStart.main(Native Method) 

@JessYanCoding
Copy link
Owner

看报错是应该是inflate xml时出现的错误,你可以贴一下你的xml,好像是LinearLayout有问题

@jp1017
Copy link
Author

jp1017 commented Nov 16, 2016

就一个简单的TextView


配置了下鸿神的AutoLayout, 这个异常没有了,又出现新问题,项目能跑,但是TextView 不显示

    @Nullable
    @BindView(R.id.tv_hello)
    TextView mTextView;

然而,去掉@nullable,这样:

    @BindView(R.id.tv_hello)
    TextView mTextView;

出现新异常:这个@nullable是必须的吗?

java.lang.IllegalStateException: Required view 'tv_hello' with ID 2131492994 for field 'mTextView'
was not found. If this view is optional add '@nullable' (fields) or '@optional' (methods) annotation.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Required view 'tv_hello' with ID 2131492994
for field 'mTextView' was not found. If this view is optional add '@nullable' (fields) or '@optional' (methods) annotation.
at butterknife.internal.Utils.findRequiredView(Utils.java:138)
at butterknife.internal.Utils.findRequiredViewAsType(Utils.java:150)
at com.inst.instcallcar.ui.activity.MainActivity_ViewBinding.(MainActivity_ViewBinding.java:21)

@JessYanCoding
Copy link
Owner

关于这个问题我又去看了下butterKnife的自动生成的源码
如果加@nullable的话它会在绑定的时候调第一个方法,如果不加的话它是调第二个方法,看源码我们知道如果第一个方法没找到id不会报错,但是第二个方法没找到就会报错,刚好这个错和你的错误吻合,所以这个错误原因是你的id没find到,可以加@nullable来防止这种报错

public static <T> T findOptionalViewAsType(View source, @IdRes int id, String who,
      Class<T> cls) {
    View view = source.findViewById(id);
    return castView(view, id, who, cls);
  }

  public static View findRequiredView(View source, @IdRes int id, String who) {
    View view = source.findViewById(id);
    if (view != null) {
      return view;
    }
    String name = getResourceEntryName(source, id);
    throw new IllegalStateException("Required view '"
        + name
        + "' with ID "
        + id
        + " for "
        + who
        + " was not found. If this view is optional add '@Nullable' (fields) or '@Optional'"
        + " (methods) annotation.");
  }

@jp1017
Copy link
Author

jp1017 commented Nov 16, 2016

恩,是这样的,怎么找不到呢?直接继承 AppcompatActivity 是正常的

@JessYanCoding
Copy link
Owner

你是不是改initView方法的时候改错了,id没错的话,可能是,你activity,setConentView的layout和你的声明这个id的Layout不是同一个

@jp1017
Copy link
Author

jp1017 commented Nov 16, 2016

不是,我就这一个布局文件,返回View和id都不行,原因还是没找到

@jp1017
Copy link
Author

jp1017 commented Nov 16, 2016

神奇的好了 😄

我是这么操作的:

  1. 重命名 id 为 tv_hello_hi, 运行,正常
  2. 恢复原来id 为 tv_hello, 运行,竟然也正常。。。

都没有使用 @nullable 注解,出现异常有意思一些。哈哈

黄油刀让人又爱又恨啊

@jp1017
Copy link
Author

jp1017 commented Nov 16, 2016

回想整个过程:

最开始的问题,xml inflate 异常是没有配置鸿神的库,这之间我用黄油刀添加了TextView

    @BindView(R.id.tv_hello)
    TextView mTextView;

然后配置好鸿神的库之后,如果上面代码去掉,问题就好解决了, 哈哈

谢谢了哈 👍

@jp1017 jp1017 closed this as completed Nov 16, 2016
@JessYanCoding
Copy link
Owner

没事,解决了就好

@rexydimen
Copy link

我新建的Demo也出现了这个问题,初始的activity_main就一个hello world的TextView,然后继承arms的BaseActivity在混淆的环境下必崩溃

@rexydimen
Copy link

问题补充下,首先我不需要butterknife,所以没向楼上那样折腾,经过我反复测试,我发现崩溃的关键点是:混淆,以及3.6.1gragle插件。

@JessYanCoding
Copy link
Owner

@rexydimen 你用 AS 3.5应该就没问题,可能是 AS3.6.1 不兼容旧代码

@JessYanCoding
Copy link
Owner

@rexydimen 这个问题这里讨论,#358

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants