Как программно добавить MaterialCardView из BottomsheetDialog в MainActivity? - PullRequest
0 голосов
/ 18 апреля 2020

У меня есть фрагмент диалага нижней таблицы. Моя цель - закрыть нижнюю таблицу, когда я нажимаю кнопку на нижней таблице и программно добавить один CardView на главный экран. Я получаю эту ошибку 'void android .widget.LinearLayout.addView (android .view.View) 'для нулевой ссылки на объект.

вот мои коды;

это - action_content. xml, подключено к Activity_main. xml

<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:backgroundTint="?attr/bottombarcolor"
    android:elevation="4dp">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/mtrl_theme_switcher_menu"
        app:titleTextColor="?attr/textcolor"
        android:backgroundTint="?attr/bottombarcolor"
        android:elevation="4dp"/>
</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:background="?attr/backgroundColor">
    <LinearLayout
        android:id="@+id/ll_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:padding="16dp"
        android:orientation="vertical"
        android:background="?attr/backgroundColor">
    </LinearLayout>
</androidx.core.widget.NestedScrollView>
</merge>

это для нижнего листа

public class BottomSheetNewTask extends BottomSheetDialogFragment  {
View bottomSheetInternal;
MaterialButton btn_apply,btn_back;
MaterialCardView mCardview;
LinearLayout.LayoutParams layoutparams;
TextView textview;
LinearLayout ll_content;

public BottomSheetNewTask() {

}

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

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

    Objects.requireNonNull(getDialog()).setOnShowListener(dialog -> {
        BottomSheetDialog d = (BottomSheetDialog) dialog;
        bottomSheetInternal = d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
        assert bottomSheetInternal != null;

         ll_content=bottomSheetInternal.findViewById(R.id.ll_content);

        btn_apply=bottomSheetInternal.findViewById(R.id.btn_apply);
        btn_apply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                addMcardview();

                d.dismiss();

            }
        });
        BottomSheetBehavior.from(bottomSheetInternal).setState(BottomSheetBehavior.STATE_EXPANDED);
    });
    return inflater.inflate(R.layout.addtasks, container, false);
}

public void addMcardview(){
    mCardview=new MaterialCardView(Objects.requireNonNull(getContext()));
    layoutparams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
    );
    mCardview.setLayoutParams(layoutparams);
    textview=new TextView(getContext());
    textview.setLayoutParams(layoutparams);
    textview.setText("deneme");
    mCardview.addView(textview);

    ll_content.addView(mCardview);
   }
 }

MaterialCardView, который я хочу добавить, должен быть в activity_content. xml. но я не смог установить sh связь между CardView и activity_content. xml

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...