【问题标题】:Can't get fragment to show, no view found for id无法显示片段,找不到 id 的视图
【发布时间】:2014-07-21 23:00:49
【问题描述】:

我试图让一个片段显示,其中包含一个 EditText 和一个按钮。我是使用 Fragment 的新手,所以我不确定在尝试创建 Fragment 时收到的错误消息究竟是什么意思。

我有一个扩展 Fragment 的类,这是创建 EditText 和按钮的地方。

public class EditNameFragment extends android.support.v4.app.Fragment {


    EditText editText;
    ImageButton button;

    public EditNameFragment(){

    }


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


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

        View view = inflater.inflate(R.layout.edit_name_dialog, container, false);

        editText = (EditText) view.findViewById(R.id.editTextDialog);
        button = (ImageButton) view.findViewById(R.id.submitNewItemButtonDialog);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //stuff
            }
        });

        return view;

    }

这里是edit_name_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:id="@+id/edit_name_dialog"
    >

    <EditText
        android:id="@+id/editTextDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     />

    <ImageButton
        android:id="@+id/submitNewItemButtonDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
         />


</LinearLayout>

在我的主要活动中(由于另一部分,它必须扩展 FragmentActivity)是我尝试设置我的 Fragment 的地方。我认为这与我引用的 id 有关。 看到有人在使用fragment时使用容器类,但不明白为什么会这样。

 FragmentManager fragmentManager = getSupportFragmentManager();
 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

 EditNameFragment fragment = new EditNameFragment();
 fragmentTransaction.add(R.id.edit_name_dialog, fragment, "tag");


 fragmentTransaction.commit();

我在尝试运行上面的代码时收到错误消息

No view found for id 0x7f09002a (com.myapp:id/edit_name_dialog) for fragment EditNameFragment

如果有人能解释我在这里缺少什么/为什么人们使用容器类,那就太好了。我知道有些人使用 XML 添加片段,但我想只使用 java 来做到这一点。

编辑

我添加了一个扩展 FragmentActivity 的类,遵循容器类的模型

public class EditNameFragmentActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.edit_name_fragment_container);
    }
}

setContentView 的参数应该是布局还是 id? 这是定义片段应该在哪里的 xml 文件

edit_name_fragment_container.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <fragment android:name="com.returnjump.spoilfoil.EditNameFragment"
        android:id="@+id/edit_name_fragment_container"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/edit_name_fragment" />

</LinearLayout>

所以对于in中的参数

fragmentTransaction.add(R.id.edit_name_dialog, fragment, "tag");

这应该引用片段的 id,对吗?

它仍然给我同样的错误,我错过了什么?

【问题讨论】:

    标签: java android xml android-fragments


    【解决方案1】:

    基本上有两种方法可以将片段添加到文档中所说的活动:

    • “静态”:通过在 Activity 的布局文件中声明片段。
    • “动态”:以编程方式添加片段。就像你试图做的那样。

    这里是文档:http://developer.android.com/guide/components/fragments.html

    如果您想动态添加,这里是您要阅读的文档部分:

    在您的 Activity 运行期间,您可以随时将片段添加到您的 Activity 布局中。您只需要指定一个 ViewGroup 来放置片段。 要在您的活动中进行片段事务(例如添加、删除或替换片段),您必须使用来自 FragmentTransaction 的 API。您可以像这样从 Activity 中获取 FragmentTransaction 的实例:

    FragmentManager fragmentManager = getFragmentManager()
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    

    然后您可以使用 add() 方法添加片段,指定要添加的片段和插入片段的视图。例如:

    ExampleFragment fragment = new ExampleFragment();
    fragmentTransaction.add(R.id.fragment_container, fragment);
    fragmentTransaction.commit();
    

    传递给 add() 的第一个参数是应该放置片段的 ViewGroup,由资源 ID 指定,第二个参数是要添加的片段。 使用 FragmentTransaction 进行更改后,必须调用 commit() 才能使更改生效。

    关于为什么要使用动态片段而不是静态片段,它是为交互式 UI 制作的,允许您根据需要简单地将不同的片段处理到一个活动中。

    【讨论】:

    • 我在我的问题中添加了更多内容,我想知道 add 方法的参数
    • 一切都在文档中。您正在尝试将片段添加到他自己的布局中,这没有意义。如果要动态添加,可以在activity的layout中创建一个FrameLayout,并将它的id作为参数用于ft.add(int res, Fragment fragment) 阅读android doc末尾的示例,都在在那里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多