【问题标题】:Android Fragment giving "Error inflating class fragment"Android片段给出“错误膨胀类片段”
【发布时间】:2013-08-02 22:12:35
【问题描述】:

我正在尝试在活动中实现一个基本片段,但它抛出了一个错误:

这是我的课程:

片段类:

public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout._fragment_dds_image, container, false);
    return view;
}

}

我的片段 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="match_parent"
android:background="#0000FF"
android:orientation="vertical" >  

我正在尝试显示片段的活动:

public class DDSViewActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ddsview);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.ddsview, menu);
    return true;
}}

活动的 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" >

<fragment
    android:id="@+id/fragment1"
    android:name="dds.MyFragment"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" />

当我运行它时,它给了他们以下错误:

无法启动 Activity ComponentInfo{com.example.electronicmenu/dds.DDSViewActivity}:android.view.InflateException:二进制 xml 文件第 10 行:膨胀类片段时出错。

link to full stack trace:

【问题讨论】:

标签: android android-fragments


【解决方案1】:

使用片段类的完全限定名称,例如com.example.myapp.dds.MyFragment

<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" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.myapp.dds.MyFragment"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />

【讨论】:

  • 如何找到我的完全限定名?我试着自己输入,但它给出了同样的错误
  • 这是你的片段类名,包括包。
  • 还是不行。我什至将 MyFragment 移到了默认包中。然后将 xml 更改为:`android:name="com.example.electronicmenu.MyFragment"`
  • 您的 Fragment 类的 .java 文件的第一行是什么,尤其是 import 语句?应该说package,后跟包名。所以全名是package_name.MyFragment
  • 好的,现在这个名字肯定是正确的。而且我仍然遇到同样的错误?
猜你喜欢
  • 2014-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-15
  • 1970-01-01
  • 2017-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多