我有一个带有按钮的 FragmentActivity,单击该按钮时,我正在显示一个 dailog
public class FragMainActivity extends FragmentActivity implements android.view.View.OnClickListener {
Button shoBut;
public String DailogTag="dialog";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shoBut= (Button) findViewById(R.id.button1);
shoBut.setOnClickListener(this);
}
@Override
public void onClick(View v) {
MyDialogFragment newFragment = MyDialogFragment.newInstance();
FragmentManager fm ;
fm=getSupportFragmentManager();
newFragment.show(fm, DailogTag);
}
}
我的对话类
public class MyDialogFragment extends DialogFragment {
static MyDialogFragment newInstance() {
return new MyDialogFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inView= inflater.inflate(R.layout.dialog_layout, container, false);
return inView;
}
}
这是我的 dialog_layout xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingBottom="36dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="36dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Hi, i am the dailog BOX...Boom"
android:textAppearance="?android:textAppearanceMedium" />
</LinearLayout>
我正在正确获取 dailog 但是我有一个 Fragment 类,ExampleFragment 像这样
public class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.frag_content, container, false);
}
}
谁的布局
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Fragment Conttent"
android:textAppearance="?android:textAppearanceMedium" />
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
当我尝试将此片段放在我的 dailog 布局中时,我的应用程序崩溃了。
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="You really want to read this text?"
android:textAppearance="?android:textAppearanceMedium" />
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class="com.example.addfragment.ExampleFragment"
android:id="@+id/example_fragment"
android:layout_width="match_parent" android:layout_height="match_parent" />
</LinearLayout>
当我将它放在我的 activity_main 布局中时,它会显示片段,但在 dailogFragment 中同样不起作用。
我正进入(状态
08-28 15:44:09.969: E/AndroidRuntime(438): android.view.InflateException: Binary XML file line #22: Error inflating class fragment
有人对此有什么建议吗?为什么 dailogFragment 布局中不能包含片段?
为什么我们不能在 daiologFragment 中添加片段?