繁体   English   中英

无法显示片段,找不到ID的视图

[英]Can't get fragment to show, no view found for id

我正在尝试显示一个片段,其中包含一个EditText和一个按钮。 我是使用片段的新手,因此我不确定在尝试创建片段时收到的错误消息的确切含义。

我有一个扩展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有关。 我见过一些人在使用片段时使用容器类,但是我不明白为什么这样做。

 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>

所以对于参数

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

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

它仍然给我同样的错误,我想念什么?

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

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

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

如果要动态添加,请阅读以下文档部分:

活动运行时,您可以随时将片段添加到活动布局中。 您只需要指定一个放置片段的V​​iewGroup。 要在活动中进行片段事务(例如添加,删除或替换片段),必须使用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()的第一个参数是应在其中放置片段的V​​iewGroup,由资源ID指定,第二个参数是要添加的片段。 使用FragmentTransaction进行更改后,必须调用commit()才能使更改生效。

关于为什么使用动态片段而不是静态片段的问题,它是为交互式UI设计的,它使您可以根据需要将不同的片段简单地处理为一个活动。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM