如何将片段放入具有自定义视图的自定义对话框中?

时间:2016-02-07 16:27:17

标签: android

您好我使用自己的Dialog类和自定义布局 其他一切工作正常 我使用了listviews gridviews等等 但是当我尝试以编程方式在对话框View中放置一个片段时,它会崩溃 与

  java.lang.IllegalArgumentException: No view found for id 0x7f0d0167 (com.myapp.app:id/frame1) for fragment CaldroidFragment{448be1b8 #0 id=0x7f0d0167}

我想将https://github.com/roomorama/Caldroid用于日历。

当我把它放在布局

中时
<fragment
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            class="com.roomorama.caldroid.CaldroidFragment"
            />

它可以工作,但我不能以编程方式获得它 与findbytag或findbyid 然后,如果我尝试以编程方式将其放在FrameLayout中,那么它会与上面的IllegalArgumentException崩溃。

根据我的想法,活动实例支持片段管理器只知道它自己的视图层次结构中的片段并忽略了Dialog的View hieararchy但是如何以编程方式获取此片段以便与它进行交互?

public class DialogFiltersDates {
Dialog dialog;

RelativeLayout rl;
private TextView tv_title;
private TextView tv_messge;
private TextView left_button;
private TextView right_button;
    Act_filters father;
    private FrameLayout fr_Dates;
    private FrameLayout fr1;

    /*******
     * Constructor
     * @param ctx
     */
public DialogFiltersDates(Context ctx){
    father = (Act_filters)ctx;
    if (dialog==null){
        dialog = new Dialog(ctx, R.style.AlertDialogCustom);
         dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.dialog_filters_date);
            dialog.setCancelable(true);
            dialog.setCanceledOnTouchOutside(false);
    }
}

    /*****
     * Showers
     * @param context
     * @param title
     * @param message

     */
public void show_notice(final Context context, String title, String message, final Runnable task_for_left,final Runnable task_for_right) {
        work_on_dialog_init(context, title, message, task_for_left, task_for_right);
    }


    /*******
     * Initializer
     * @param context
     * @param title
     * @param message
     * @param task_for_left

     */
private void work_on_dialog_init(final Context context, String title, String message, final Runnable task_for_left, final Runnable task_for_right){


    if (dialog!=null) {
        Log.i("DialogNotices", "dialog was already opened-->"+dialog.getContext().getClass().toString());
        Log.i("DialogNotices","trying to open from-->"+context.getClass().toString());
    }
    //if is null or not from this activity
    if (dialog==null || !dialog.getContext().getClass().toString().equals(context.getClass().toString())){
        dialog=null;
        dialog = new Dialog(context,R.style.AlertDialogCustom);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_filters_date);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                dialog=null;
            }
        });
    }
    else {
        dialog.dismiss();
    }
     left_button = (TextView) dialog.findViewById(R.id.left_btn);
     right_button = (TextView) dialog.findViewById(R.id.right_btn);

//   rl = (RelativeLayout) dialog.findViewById(R.id.rlmain);
//  this.tv_title = (TextView) dialog.findViewById(R.id.message_title);
//  this.tv_messge = (TextView) dialog.findViewById(R.id.message);
     fr1 = (FrameLayout)dialog.findViewById(R.id.frame1);
//  right_button.setText("Ok");
//
    left_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            dialog.dismiss();
            if (task_for_left != null) {
                task_for_left.run();
            }
        }
    });

    right_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            dialog.dismiss();
            if (task_for_right != null) {
                task_for_right.run();
            }

        }
    });
    if ( !( (Activity) (context) ).isFinishing() ){
        dialog.show();
    }
    setupCalendar();
}

    private void setupCalendar() {
        CaldroidFragment caldroidFragment = new CaldroidFragment();
        Bundle args = new Bundle();
        Calendar cal = Calendar.getInstance();
        args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
        args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
        android.support.v4.app.FragmentTransaction t = father.getSupportFragmentManager().beginTransaction();

        //List<Fragment> asda = father.getSupportFragmentManager().getFragments();
        t.replace(R.id.frame1, caldroidFragment);
        t.commit();
        CaldroidFragment caldroidFragment2 = (CaldroidFragment) father. getSupportFragmentManager().
            findFragmentByTag("CaldroidFragment");
                //findFragmentById(R.id.frag_date2);

        if (caldroidFragment!=null) {
            caldroidFragment.setArguments(args);
        }


    }

    public void dismiss(){
        this.dialog.dismiss();
    }

}

2 个答案:

答案 0 :(得分:0)

  

当我把它放在布局

中时
<fragment
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    class="com.roomorama.caldroid.CaldroidFragment"
/>
  

它有效,但我不能用findbytag或编程方式获得它   如果我尝试以编程方式将其放入abyby中再次找到   例如,FrameLayout,然后它与上面的方法崩溃。

您是否为片段提供了布局的唯一ID?没有它,就无法找到视图。

android:id=@+id/my_id

答案 1 :(得分:0)

好的,我这样解决了。

我从片段管理器中删除所有片段 每次我实施对话 因为当片段膨胀布局时,片段也有一个实例。

type CarService struct {
    dbName         string
    uri            string
    collectionName string
}

func (r CarService) New() *CarService {
    configManger := viperconfing.Config{}
    r.uri = configManger.GetConfig("dbUri")
    r.dbName = configManger.GetConfig("dbName")
    r.collectionName = "car"
    return &r
}

func (r CarService) Find(query *bson.M) (cars []models.Car) {
    session, _ := mgo.Dial(r.uri)
    defer session.Close()
    session.SetSafe(&mgo.Safe{})
    collection := session.DB(r.dbName).C(r.collectionName)

    collection.Find(query).All(&cars)

    fmt.Println(cars)
    return cars
}