【问题标题】:Button click listener in fragment. Android studio片段中的按钮单击侦听器。安卓工作室
【发布时间】:2018-10-16 22:38:35
【问题描述】:

我在尝试在片段中实现按钮单击侦听器时遇到问题。如果只有一个按钮,我没有问题,但片段 UI 中有 3 个按钮。

这是我得到的:

  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

代码,我把它做成了公共方法

@Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
      // Inflate the layout for this fragment
       View view = inflater.inflate(R.layout.fragment_blank, container, false);


  first=(Button) view.findViewById(R.id.button_first);
  first.setOnClickListener(this);
  second=(Button) view.findViewById(R.id.button_second);
  second.setOnClickListener(this);
  third=(Button) view.findViewById(R.id.button_third);
  third.setOnClickListener(this);

  // Also I tried these ones but its not possible to :
  //first.setOnClickListener((View.OnClickListener) getActivity());
  //second.setOnClickListener((View.OnClickListener) getActivity());
  //third.setOnClickListener((View.OnClickListener) getActivity());


    return view;
}

 @Override
    public void onClick(View view) {

    if (view.getId() == R.id.button_first) {

     //some code here
    }
    if (view.getId() == R.id.button_second) {
        //some code here
    }
    if (view.getId() == R.id.button_third) {
       //some code here
        }
 }

我可以实现一个按钮监听器如下:

@Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_blank_fragment5, container, false);


  call= (Button) view.findViewById(R.id.button);
  call.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v)
     {
         dialContactPhone("Some phone number");

     }

 });
      return view;
 }

但我想知道我在第一个代码中做错了什么。为什么会有null

【问题讨论】:

  • 你声明你的按钮了吗?
  • 看来button_firstbutton_secondbutton_third 中的至少一个不在fragment_blank 布局中。
  • 是的,它们是在 xml 布局中定义的,具有相应的 id。我总是将 xml 项 id 复制到 findViewById。我不知道发生了什么,因为它是一个简单的代码。
  • 你确定那些Buttons 正好在fragment_blank 中吗?他们必须在那个,特别是。如果是这样,您是否有多个版本的 fragment_blank - 例如,针对不同的方向、不同的 Android 版本等 - 所有这些 Buttons 可能不在每个版本中?

标签: android button fragment


【解决方案1】:

您没有在第一个示例中提供片段类代码,但由于您使用 .setOnClickListener(this) 并在代码中出现错误,我假设您没有在您的类中实现 OnClickListener。如果您使用“this”来设置侦听器并且您没有在您的类中实现OnClickListener,那么 setOnClickListener() 将找不到任何内容。这就是导致您的NullPointerException 的原因。

如果你想使用“this”来设置你的监听器并使用 onClick(View view) 方法来管理你的点击,你的类必须实现 OnClickListener。

示例:

public class MyFragmentClass extends Fragment implements OnClickListener {
    // You can use "this" for .setOnClickListener() in this class
}

【讨论】:

  • 这会导致编译时错误。 OP 甚至都无法运行该应用程序,更不用说获得运行时 Exception
  • @Mike M.,请解释一下。您是指我忘记放置“扩展片段”的事实吗? (我稍后会编辑),或者您指的是其他问题。
  • 不,我的意思是这个答案没有解决给定Exception 及其消息中显示的问题。
【解决方案2】:

试试这个

  • fragmet_blank xml 你可以定义 3 Button like button_first , button_second, button_third 使用相同的 id。

java 代码

public class BlankFragment extends Fragment implements OnClickListener{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.fragment_blank, container, false);

Button f = (Button) v.findViewById(R.id.button_first);
Button s = (Button) v.findViewById(R.id.button_second);
Button t = (Button) v.findViewById(R.id.button_third); 
f.setOnClickListener(this);
s.setOnClickListener(this);
t.setOnClickListener(this);
return v;
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_first:

   //some code here

    break;
}

  case R.id.button_second:

   //some code here

    break;
  }
 case R.id.button_third:

   //some code here

    break;
}
 }
   } 

【讨论】:

    【解决方案3】:

    这对我来说很好用。 为每个按钮单独设置点击监听器。

    View rootView = inflater.inflate(R.layout.fragment_default, container, false);
    
            rootView.findViewById(R.id.sell)
                .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Your code
                }
            });
            rootView.findViewById(R.id.buy)
                .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Your Code
                }
    
            });
            rootView.findViewById(R.id.bookbuddy)
                .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Your Code
                }
            });
            rootView.findViewById(R.id.listing)
                .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Your Code
                }
            });
    
    return rootView;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 2017-11-09
      相关资源
      最近更新 更多