在活动中的自定义对话框内的ANDROID片段

时间:2016-07-01 11:17:36

标签: java android android-fragments android-studio fragment

我想在活动中的自定义对话框中放入一个片段。

我收到此错误:

java.lang.IllegalArgumentException: No view found for id 0x7f0c007e (copyworld.rebootcw:id/container_schedule1) for fragment FragmentGiorno{418215c0 #0 id=0x7f0c007e}

这是我的代码:

layout.xml

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_below="@+id/spinner_dialog_schedulazione"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:id="@+id/container_schedule1">

</FrameLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {


      @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

 Popup_schedule();
}


//******POPUP PER LA SCHEDULAZIONE******************

public void Popup_schedule() {
    dialog_schedule=new Dialog(MainActivity.this);
    dialog_schedule.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    dialog_schedule.setContentView(R.layout.dialog_schedule);
    dialog_schedule.setCancelable(false);
    lp = new WindowManager.LayoutParams();
    window = dialog_schedule.getWindow();
    lp.copyFrom(window.getAttributes());
    lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    dialog_schedule.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    window = dialog_schedule.getWindow();
    window.setAttributes(lp);


                 FragmentGiorno fragment = new FragmentGiorno();
                 android.support.v4.app.FragmentTransaction fragmentTransaction =
                         getSupportFragmentManager().beginTransaction();
                 fragmentTransaction.replace(R.id.container_schedule1, fragment);
                 fragmentTransaction.commit();

    dialog_schedule.show();

}
}

如何在扩展活动的 MainActivity.java 的自定义对话框中添加片段?

2 个答案:

答案 0 :(得分:0)

试试这个

 dialog_schedule.findViewById(R.id.container_schedule1)

取代

 R.id.container_schedule1

在您的代码中

答案 1 :(得分:0)

  

如果要在对话框中加载片段,则可以改为使用DialogFragment

以下是使用DialogFragment

的工作示例
public class Demo extends DialogFragment{

View mainView;

LinearLayout layoutArrow;

Bundle bundle;
ArrayList<BookingDetail> bookingDetails;
View.OnClickListener onClickListener;
public static Demo newInstance(@NonNull ArrayList<BookingDetail> bookingDetails) {
    Demo alertPopUpMapReservedBedInfo = new Demo();
    Bundle args = new Bundle();
    args.putSerializable("bookingDetails", bookingDetails);
    alertPopUpMapReservedBedInfo.setArguments(args);

    return alertPopUpMapReservedBedInfo;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_TITLE, R.style.InvitationDialogWithSimpleAnim);
    bundle = getArguments();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    mainView = inflater.inflate(R.layout.alert_info, container, false);
    getDialog().setCanceledOnTouchOutside(true);

    return mainView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    init();
}

private void init() {
    layoutArrow = (LinearLayout) mainView.findViewById(R.id.layoutArrow);

 FragmentGiorno fragment = new FragmentGiorno();
             android.support.v4.app.FragmentTransaction fragmentTransaction =
                     this.getChildFragmentManager().beginTransaction();
             fragmentTransaction.replace(R.id.container_schedule1, fragment);
             fragmentTransaction.commit();
}



@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    if(onClickListener!=null){
        onClickListener.onClick(null);
    }
}
}

然后叫它,

    Demo newFragment = Demo.newInstance(bookingDetails);
    newFragment.show(ft,"dialog");