简体   繁体   English

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

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

I am trying to get a fragment to show that contains an EditText and a button. 我正在尝试显示一个片段,其中包含一个EditText和一个按钮。 I am new to using fragments, so I am not sure exactly what the error message I get when trying to create the fragment means. 我是使用片段的新手,因此我不确定在尝试创建片段时收到的错误消息的确切含义。

I have a class that extends Fragment, this is where the EditText and button are created. 我有一个扩展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;

    }

Here is edit_name_dialog.xml 这是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>

And here in my main activity (which must extend FragmentActivity because of another part) is where I try to set up my Fragment. 在我的主要活动(由于另一部分而必须扩展FragmentActivity)中,我尝试设置Fragment。 I think it has something to do with what id I am referencing. 我认为这与我引用的ID有关。 I have seen some people using container classes when using fragments, but I do not understand why this is done. 我见过一些人在使用片段时使用容器类,但是我不明白为什么这样做。

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

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


 fragmentTransaction.commit();

I get the error message when trying to run the code above 尝试运行以上代码时收到错误消息

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

If anyone could explain what I am missing here/ why people use container classes, that would be great. 如果有人能解释我在这里缺少的内容/为什么人们使用容器类,那将很棒。 I know some people add fragments using XML, but I would like to do this only using java. 我知道有些人使用XML添加片段,但是我只想使用Java来做到这一点。

EDIT 编辑

I have added a class that extends FragmentActivity, following the model for a container class 我在容器类的模型之后添加了一个扩展FragmentActivity的类

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

        setContentView(R.layout.edit_name_fragment_container);
    }
}

Is the parameter for setContentView supposed to be the layout, or an id? setContentView的参数应该是布局还是id? Here is the xml file that defines where the fragment should be 这是xml文件,用于定义片段的位置

edit_name_fragment_container.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>

So for the parameter in 所以对于参数

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

this is supposed to reference the id of the fragment, correct? 这应该引用片段的ID,对吗?

It still gives me the same error, what am I missing? 它仍然给我同样的错误,我想念什么?

There are basically two ways to add a fragment to an activity like the documentation say: 基本上有两种方法可以将片段添加到活动中,如文档所述:

  • "statically": by declaring the fragment inside the activity's layout file. “静态”:通过声明活动布局文件中的片段。
  • "dynamically": adding the fragment programmatically. “动态”:以编程方式添加片段。 Like you tried to do. 就像您尝试做的那样。

Here is the documentation: http://developer.android.com/guide/components/fragments.html 这是文档: http : //developer.android.com/guide/components/fragments.html

If you wish to add it dynamically, here is the documentation part that you want to read: 如果要动态添加,请阅读以下文档部分:

At any time while your activity is running, you can add fragments to your activity layout. 活动运行时,您可以随时将片段添加到活动布局中。 You simply need to specify a ViewGroup in which to place the fragment. 您只需要指定一个放置片段的V​​iewGroup。 To make fragment transactions in your activity (such as add, remove, or replace a fragment), you must use APIs from FragmentTransaction. 要在活动中进行片段事务(例如添加,删除或替换片段),必须使用FragmentTransaction中的API。 You can get an instance of FragmentTransaction from your Activity like this: 您可以从Activity中获取FragmentTransaction的实例,如下所示:

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

You can then add a fragment using the add() method, specifying the fragment to add and the view in which to insert it. 然后,您可以使用add()方法添加片段,指定要添加的片段以及要在其中插入的视图。 For example: 例如:

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

The first argument passed to add() is the ViewGroup in which the fragment should be placed, specified by resource ID, and the second parameter is the fragment to add. 传递给add()的第一个参数是应在其中放置片段的V​​iewGroup,由资源ID指定,第二个参数是要添加的片段。 Once you've made your changes with FragmentTransaction, you must call commit() for the changes to take effect. 使用FragmentTransaction进行更改后,必须调用commit()才能使更改生效。

And about why to use dynamic fragments instead of static fragments, it has been made for interactive UI allowing you to simply handle different fragments into one activity as you please. 关于为什么使用动态片段而不是静态片段的问题,它是为交互式UI设计的,它使您可以根据需要将不同的片段简单地处理为一个活动。

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

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