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

时间:2017-07-14 16:28:01

标签: java android

我有自定义BottomSheetDialogFragment,我想从我的TextView设置MainActivity。我试图获得对其视图的引用,但我得到了一个空指针异常。我从我的Fragment创建MainActivity,如下所示:

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");
}

这是我的BottomSheetDialogFragment类:

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>

如何正确获取textview的引用?谢谢!

编辑:这是错误:

java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                 at android.view.View$DeclaredOnClickListener.onClick(View.java:4725)
                                                                                 at android.view.View.performClick(View.java:5637)
                                                                                 at android.view.View$PerformClick.run(View.java:22429)
                                                                                 at android.os.Handler.handleCallback(Handler.java:751)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                              Caused by: java.lang.reflect.InvocationTargetException
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at android.view.View$DeclaredOnClickListener.onClick(View.java:4720)
                                                                                 at android.view.View.performClick(View.java:5637) 
                                                                                 at android.view.View$PerformClick.run(View.java:22429) 
                                                                                 at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                 at android.os.Looper.loop(Looper.java:154) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                                                                                 at com.gofrontrow.conductor_mobile.MainActivity.buttonClick(MainActivity.java:108)
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at android.view.View$DeclaredOnClickListener.onClick(View.java:4720) 
                                                                                 at android.view.View.performClick(View.java:5637) 
                                                                                 at android.view.View$PerformClick.run(View.java:22429) 
                                                                                 at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                 at android.os.Looper.loop(Looper.java:154) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

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

1 个答案:

答案 0 :(得分:0)

我提出了一种不同的方法,只需在你的BottomSheetDialogFragment内部创建一个名为buttonClick的方法或者其他方法。在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) sheetView.findViewById(R.id.cmp1);
return v;

现在您已经引用了sheetText TextView,您可以创建一个正常的方法,如下所示:

void onButtonClick(String text){
    sheetText.setText(text);
}

你可以像MainActivity那样叫这个:

public void buttonClick(View view) {
    CustomBottomSheetDialogFragment bottomSheetDialogFragment = new CustomBottomSheetDialogFragment();
    bottomSheetDialogFragment.show(getSupportFragmentManager(), "Dialog");
    bottomSheetDialogFragment.onButtonClick("whatever);
}

另外,如果您不想在MainActivity中单击按钮上不断更改文本,您还可以将参数传递给构造函数,如果我误解了您要执行的操作。