1

我有一个用于bottomsheetdialog片段的类。我看了很多地方,但我很困惑。我想更改底部表格中按钮的文本。我收到此错误'android.view.View android.view.View.findViewById(int) ' 在空对象引用上。这是我的代码;

 public class MainActivity extends AppCompatActivity {

    @Override
     protected void onCreate(final Bundle savedInstanceState) {

       bottomSheetFragment=new BottomSheetFragment();
       View viewDialog=bottomSheetFragment.getView();
       assert viewDialog != null;
       MaterialButton btn_titresim=viewDialog.findViewById(R.id.btn_titresim);
       btn_titresim.setText("text");
     }
 }

BottomSheetDialogFragment 的另一个类

public class BottomSheetFragment extends BottomSheetDialogFragment {
public BottomSheetFragment() {}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Objects.requireNonNull(getDialog()).setOnShowListener(dialog -> {

        BottomSheetDialog d = (BottomSheetDialog) dialog;
        View bottomSheetInternal = 
            d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
        assert bottomSheetInternal != null;
        BottomSheetBehavior.from(bottomSheetInternal).setState(BottomSheetBehavior.STATE_EXPANDED);
    });
    return inflater.inflate(R.layout.layout_popup, container, false);
}

}

4

1 回答 1

2

您可以通过在片段中添加一个侦听器来解决此问题,该侦听器interface将片段返回BottomSheetView您的活动,因此您可以通过方法BottomSheetDialogFragment正常访问底层视图。findViewById()

在这里,我决定使用Singleton模式BottomSheetDialogFragment从活动中设置侦听器实例。

所以在你的片段中添加一个监听器;它在下面命名FragmentListener,调用监听回调 inonCreateView()或 inonViewCreated()

public class BottomSheetFragment extends BottomSheetDialogFragment {

    public BottomSheetFragment() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    interface FragmentListener {
        void getView(View view);
    }

    static FragmentListener mFragmentListener;

    public static BottomSheetFragment newInstance(FragmentListener listener) {
        mFragmentListener = listener;
        return new BottomSheetFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Objects.requireNonNull(getDialog()).setOnShowListener(dialog -> {

            BottomSheetDialog d = (BottomSheetDialog) dialog;
            View bottomSheetInternal = 
                d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
            assert bottomSheetInternal != null;
            BottomSheetBehavior.from(bottomSheetInternal).setState(BottomSheetBehavior.STATE_EXPANDED);
        });

        View view = inflater.inflate(R.layout.layout_popup, container, false);

        // Trigger the listener callback to return the view back to the activity
        // mFragmentListener.getView(view);  // Not working in all devices

        return inflater.inflate(R.layout.layout_popup, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        // Trigger the listener callback to return the view back to the activity
        mFragmentListener.getView(view);
    }

}

通过您的活动实现侦听器,并更改回调中的文本,并BottomSheetDialogFragment使用单例模式实例化。

 public class MainActivity extends AppCompatActivity implements BottomSheetFragment.FragmentListener {

    @Override
     protected void onCreate(final Bundle savedInstanceState)  {

       bottomSheetFragment = BottomSheetFragment.newInstance(this);

     }

    @Override
    public void getView(View view) {
        // Setting the text
        ((MaterialButton) view.findViewById(R.id.btn_titresim)).setText("text");
    }

 }

希望能解决你的问题

于 2020-04-13T23:50:22.593 回答