Java 如何在BottomSheetDialogFragment内的TextView上设置文本

Java 如何在BottomSheetDialogFragment内的TextView上设置文本,java,android,Java,Android,我有一个自定义的BottomSheetDialogFragment,我想从我的main活动设置它的TextView。我试图获取对其视图的引用,但得到一个空指针异常。我从我的main活动创建片段,如下所示: public void buttonClick(View view) { CustomBottomSheetDialogFragment bottomSheetDialogFragment = new CustomBottomSheetDialogFragment(); Vie

我有一个自定义的
BottomSheetDialogFragment
,我想从我的
main活动设置它的
TextView
。我试图获取对其视图的引用,但得到一个空指针异常。我从我的
main活动创建
片段
,如下所示:

public void buttonClick(View view) {
    CustomBottomSheetDialogFragment bottomSheetDialogFragment = new CustomBottomSheetDialogFragment();
    View sheetView = bottomSheetDialogFragment.getView();
    TextView sheetText = (TextView) sheetView.findViewById(R.id.cmp1); 
    sheetText.setText("blah");
    bottomSheetDialogFragment.show(getSupportFragmentManager(), "Dialog");
}
下面是我的DialogFragment类:

public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.content_bottom_sheet, container, false);
        return v;
    }
}
下面是XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:id="@+id/bottomSheetLayout"
   android:layout_width="match_parent"
   android:layout_height="300dp"
   android:background="@android:color/white"
   android:padding="16dp"
   app:behavior_hideable="true"
   app:behavior_peekHeight="60dp"
   app:layout_behavior="@string/bottom_sheet_behavior">

   <TextView
       android:id="@+id/bottomSheetHeading"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Test"
       android:textAppearance="@android:style/TextAppearance.Large" />

   <TextView
       android:id="@+id/cmp1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@id/bottomSheetHeading"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="16dp"
       android:text="Test Message"
       android:textAppearance="@android:style/TextAppearance.Medium" />

</RelativeLayout>

最终编辑:最后,我为片段创建了一个构造函数,并以这种方式传递字符串。我使用post作为指导。

我提出了一种不同的方法,只需在
对话框片段
中创建一个名为
按钮单击
的方法。在
onCreateView
的内部,将
TextView
绑定到它的
id
上,如下所示:

View v = inflater.inflate(R.layout.content_bottom_sheet, container, false);
// declare it as a field at the top of the class
sheetText = (TextView) v.findViewById(R.id.cmp1);
return v;
public void buttonClick(View view) {
    CustomBottomSheetDialogFragment bottomSheetDialogFragment = new CustomBottomSheetDialogFragment();
    bottomSheetDialogFragment.show(getSupportFragmentManager(), "Dialog");
    bottomSheetDialogFragment.onButtonClick("whatever);
}
现在您已经有了对
sheetText
TextView的引用,您可以创建一个普通方法,如下所示:

void onButtonClick(String text){
    sheetText.setText(text);
}
您可以从
MainActivity
调用此函数,如下所示:

View v = inflater.inflate(R.layout.content_bottom_sheet, container, false);
// declare it as a field at the top of the class
sheetText = (TextView) v.findViewById(R.id.cmp1);
return v;
public void buttonClick(View view) {
    CustomBottomSheetDialogFragment bottomSheetDialogFragment = new CustomBottomSheetDialogFragment();
    bottomSheetDialogFragment.show(getSupportFragmentManager(), "Dialog");
    bottomSheetDialogFragment.onButtonClick("whatever);
}

另外,如果您不想在MainActivity中持续更改按钮单击时的文本,如果我误解了您的意图,您也可以将参数传递给构造函数。

请您粘贴
nullPointerException
错误日志好吗?谢谢,我刚刚尝试了这个方法,但仍然得到了正确答案“尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'”我知道当我只想“显示”底部表单时,它是有效的。它访问TextView时由于某种原因而崩溃。好的,我在onButtonClick和onCreateView中添加了打印语句,发生的情况是”钮扣“在调用onCreateView之前调用方法。我不知道为什么,即使onButtonClick在下面。在主活动中显示。这里发生了什么?原因是创建片段需要一些时间。尝试我提供的第二种解决方案,将参数传递给DialogFragment的构造函数,并将其保存为字段,然后在onCreateView中将textView的文本设置为该值。好吗?成功了!我确实需要研究如何避免对片段使用非默认构造函数,但您的第二个解决方案奏效了!