【发布时间】: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