【问题标题】:Nothing is reachable inside of a fragment片段内部没有任何东西可以到达
【发布时间】:2013-07-05 16:05:25
【问题描述】:

我需要在片段内操作我的按钮等,但是 findViewById 方法无法识别。此外,由于无法识别方法,甚至无法设置警报对话框。

如果我添加 getActivity() 或 getActivity().getApplicationContext() 它适用于片段绑定的活动。

实现我的对象并在片段内对其进行操作的正确方法是什么?

【问题讨论】:

  • DatePickerDialog(getActivity(),

标签: android android-fragments


【解决方案1】:

findViewById 方法继承自 Activity 类。在片段中,您可以在特定的View 对象上调用此方法。在大多数情况下,您将在膨胀视图上调用它。请记住,findViewById 是一个缓慢的操作!!将您的视图的引用保留为活动成员始终是一个好习惯。还要在最具体的View 对象上调用此方法 - 如果您想从放置在较大容器中的容器中获取视图,请在较小的容器上调用该方法 - 它更快。 :)

【讨论】:

    【解决方案2】:

    对于 Fragment,你应该重写 onCreateView 方法并膨胀你想要的视图

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View root = inflater.inflate(R.layout.your_fragment_layout, container, false);
            progressBar = (ProgressBar)root.findViewById(R.id.progress_bar);
     }
    

    【讨论】:

    • mPickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(DATE_DIALOG_ID); } });例如;无法识别 showDialog;在这里做什么?
    • 受保护的对话框 onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog("这里要添加什么", mDateSetListener, mYear, mMonth, mDay); } 返回空值;我还需要添加“在此处添加什么”字段以在片段中使用
    【解决方案3】:

    举例

    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.list_view_fragment, container, false);
        listView = (ListView)v.findViewById(R.id.listView);
        ImageButton imageBtn = (ImageButton)v.findViewById(R.id.imageButton);
        imageBtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                Builder MyAlertDialog = new AlertDialog.Builder(getActivity());
                    MyAlertDialog.setTitle("Title");
                    MyAlertDialog.setMessage("Message");
                    DialogInterface.OnClickListener deleteClick = new DialogInterface.OnClickListener()
                    {
                        public void onClick(DialogInterface dialog, int which) {
                            //your code
                        }
                    };
                    MyAlertDialog.setPositiveButton("First",deleteClick );
                    MyAlertDialog.setNeutralButton("Second",new DialogInterface.OnClickListener()
                         {
                            public void onClick(DialogInterface dialog, int which){ 
                                //second
                            }
    
                         });
                    MyAlertDialog.setNegativeButton("Last",new DialogInterface.OnClickListener()
                         {
                            public void onClick(DialogInterface dialog, int which) {
                                return;
                            }
                         });
                    MyAlertDialog.show();
    
            }
            });
        return v;
    }
    

    【讨论】:

    • 如何在片段中使用onPrepareDialog()方法?
    猜你喜欢
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 2016-09-17
    相关资源
    最近更新 更多