【问题标题】:LinearLayout as clickable in fragment java cannot resolve method findViewById片段java中可点击的LinearLayout无法解析方法findViewById
【发布时间】:2018-03-12 06:56:09
【问题描述】:

大家好,我一直在为我的项目构建自己的应用程序,因为我在其中一个片段中创建了片段,我需要通过单击整个线性布局来设置一个值。

但是当我这样做时,我在片段 java 代码findViewById 中遇到错误(无法解析方法findViewById(init))。

我被困在这个问题上,请帮帮我!

这是我在 OnCreateView 下的代码

       View view = inflater.inflate(R.layout.notification, container, false);
       LinearLayout set = (LinearLayout) findViewById(R.id.LinearLayout);
       return view;

【问题讨论】:

  • 使用view.findViewById()

标签: java android fragment


【解决方案1】:

片段

中工作时必须通过视图
View view = inflater.inflate(R.layout.notification, container, false);
LinearLayout set = (LinearLayout) view.findViewById(R.id.LinearLayout);
return view;

【讨论】:

    【解决方案2】:

    只需将代码更改为:

    View view = inflater.inflate(R.layout.notification, container, false);
    LinearLayout set = (LinearLayout)view.findViewById(R.id.LinearLayout);
    return view;
    

    当您在变量 view 中获取视图时,您只能从视图中找到它。

    【讨论】:

      【解决方案3】:

      在片段中,你可以只调用你的视图而不是调用你的线性布局id,

         View view = inflater.inflate(R.layout.notification, container, false);
         LinearLayout set = (LinearLayout) view.findViewById(R.id.LinearLayout);
         return view;
      

      【讨论】:

        猜你喜欢
        • 2015-11-25
        • 1970-01-01
        • 1970-01-01
        • 2019-05-03
        • 1970-01-01
        • 1970-01-01
        • 2021-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多