【问题标题】:Buttons in other two inflated layout return a Null Pointer Exception其他两个膨胀布局中的按钮返回空指针异常
【发布时间】:2015-09-06 14:57:58
【问题描述】:

我有一个具有三种布局的选项卡视图。每个布局使用框架布局分为 4 个部分,每个部分都有一个按钮。所以总共有12个按钮。由于 View 是 First Layout,所以其他两种布局中的按钮返回空指针异常。我尝试包含在 XML 中,但按钮被合并并且应用程序运行。任何解决方案。日夜工作。

View rootView = inflater.inflate(R.layout.fragment_main,container,false);
View rootView = inflater.inflate(R.layout.fragment_2,container,false);
View rootView = inflater.inflate(R.layout.fragment_3,container,false);

b1=(Button)rootView.findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View a){...}
});

Fragment_main 中的按钮正在运行,但 fragment_2 和 fragment_3 中的按钮返回空指针异常。

注意:我使用了 Eclipse 的选项卡布局。

提前感谢您的帮助

【问题讨论】:

    标签: android android-inflate


    【解决方案1】:

    你将 rootView 膨胀了 3 次,最后一个膨胀了。前两个布局及其子布局不可访问。

    【讨论】:

      【解决方案2】:

      修改代码如下,据我所知,它可以正常工作。

          View rootView = inflater.inflate(R.layout.fragment_main,container,false);
          View rootView1 = inflater.inflate(R.layout.fragment_2,container,false);
          View rootView2 = inflater.inflate(R.layout.fragment_3,container,false);
      
          b1=(Button)rootView.findViewById(R.id.button1);
          b1.setOnClickListener(new View.OnClickListener(){
             @Override
             public void onClick(View a){...}
          });
      
          b2=(Button)rootView1.findViewById(R.id.button2);
          b2.setOnClickListener(new View.OnClickListener(){
             @Override
             public void onClick(View a){...}
          });
      

      它对你来说很好,如果你有任何疑问,请告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-21
        • 2023-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多