GithubHelp home page GithubHelp logo

Comments (13)

JessYanCoding avatar JessYanCoding commented on May 12, 2024

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

from mvparms.

jp1017 avatar jp1017 commented on May 12, 2024

就一个简单的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)

from mvparms.

JessYanCoding avatar JessYanCoding commented on May 12, 2024

关于这个问题我又去看了下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.");
  }

from mvparms.

jp1017 avatar jp1017 commented on May 12, 2024

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

from mvparms.

JessYanCoding avatar JessYanCoding commented on May 12, 2024

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

from mvparms.

jp1017 avatar jp1017 commented on May 12, 2024

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

from mvparms.

jp1017 avatar jp1017 commented on May 12, 2024

神奇的好了 😄

我是这么操作的:

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

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

黄油刀让人又爱又恨啊

from mvparms.

jp1017 avatar jp1017 commented on May 12, 2024

回想整个过程:

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

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

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

谢谢了哈 👍

from mvparms.

JessYanCoding avatar JessYanCoding commented on May 12, 2024

没事,解决了就好

from mvparms.

rexydimen avatar rexydimen commented on May 12, 2024

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

from mvparms.

rexydimen avatar rexydimen commented on May 12, 2024

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

from mvparms.

JessYanCoding avatar JessYanCoding commented on May 12, 2024

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

from mvparms.

JessYanCoding avatar JessYanCoding commented on May 12, 2024

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

from mvparms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.