我有一个包含 fragment 的 Activity 。
Activity 看起来:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="keepInTouch.mainScreen.MainActivity">
<TextView
android:id="@+id/textV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<fragment
android:id="@+id/fragmnet_main_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_main_list" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
fragment 外观: fragment_main_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="keepInTouch.today.MainListFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/mainFragmentListTitle"
android:background="#e43d3d"
android:textColor="#371b1b" />
<ListView
android:id="@+id/lvKits"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
public class MainListFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main_list, container, false);
}
我收到以下错误: android.view.InflateException:二进制 XML 文件第 41 行:错误膨胀类 fragment
我已经尝试更换 MainActivity 扩展 AppCompatActivity 和: MainActivity 扩展了 FragmentActivity
但我得到了相同的结果。
我做错了什么?
最佳答案
您忘记了 fragment
中的 android:name
属性。这样做:
<fragment
android:id="@+id/fragmnet_main_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="keepInTouch.today.MainListFragment"
tools:layout="@layout/fragment_main_list" />
关于android - 为什么我会收到 Error inflating class fragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39297597/