如何从android中的另一个片段启用/禁用按钮?

时间:2017-07-29 17:03:14

标签: android android-fragments nullpointerexception

我有来自片段编号1的4个片段的活动我想在片段3上启用现有按钮(即禁用),当我在fragment1中单击我的按钮时。这是我的尝试:  片段1:

private void ExpandAreaOfControl()
{
    int yStart = (int)TargetLocation.Y;
    int xStart = (int)TargetLocation.X;
    int zStart = (int)TargetLocation.Z;

    int startGridSize = 20;
    for (double y = startGridSize; y < Size / 2; y += startGridSize)
    {
        for (double x = startGridSize; x < Size / 2; x += startGridSize)
        {
            for (double z = startGridSize; z < Size / 2; z += startGridSize)
            {
                var point1 = new MineralPoint(new Vector3D(xStart + x, yStart + y, zStart + z));
                mineralDeposits.Add(point1);

                var point2 = new MineralPoint(new Vector3D(xStart + x, yStart + y, zStart - z));
                mineralDeposits.Add(point2);

                var point3 = new MineralPoint(new Vector3D(xStart + x, yStart - y, zStart - z));
                mineralDeposits.Add(point3);

                var point4 = new MineralPoint(new Vector3D(xStart + x, yStart - y, zStart + z));
                mineralDeposits.Add(point4);

                var point5 = new MineralPoint(new Vector3D(xStart - x, yStart + y, zStart + z));
                mineralDeposits.Add(point5);

                var point6 = new MineralPoint(new Vector3D(xStart - x, yStart - y, zStart + z));
                mineralDeposits.Add(point6);

                var point7 = new MineralPoint(new Vector3D(xStart - x, yStart + y, zStart - z));
                mineralDeposits.Add(point7);

                var point8 = new MineralPoint(new Vector3D(xStart - x, yStart - y, zStart - z));
                mineralDeposits.Add(point8);
            }
        }
    }
    mineralDeposits = mineralDeposits.OrderBy(y => (y.Location - StartPosition).Length()).ToList();
}

这给我一个错误:

  

java.lang.NullPointerException:尝试调用虚方法&#39; void android.widget.Button.setEnabled(boolean)&#39;在空对象引用上

如何正确启用按钮并更改状态?

1 个答案:

答案 0 :(得分:1)

首先,创建一个界面。

UpdateButtonListener.java

public interface UpdateButtonListener {
    void onUpdate(boolean status);
}

现在在片段3中,实现类

的接口
public class Fragment3 extends Fragment implements UpdateButtonListener{

public static UpdateButtonListener updateButton;

@Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment3, container, false);
        updateButton = this;
        return view;
         }

    @Override
    public void onUpdate(boolean status) {
        // here set the functionality of button whether to disable or not .
        if(status){
        btnHabilitarMed.setEnabled(true);
        btnHabilitarImc.setEnabled(true);
        }
    }
 }

现在是第一个片段。

btnGuardar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Fragment3.updateButton.onUpdate(true);
  }

为其他人做同样的事。