簡體   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