我刚刚开始开发我的第一个Android应用程序,我的重点是该应用程序的主要活动MathleteIDActivity,其中包括显示一些文本的片段TextFragment

该项目可以正常运行,但是一旦启动该应用程序便会崩溃。这是在运行Android 4.x.x的两部不同手机上发生的,因此片段支持不应该成为问题。来自adb logcat的stacktrace如下所示(已编辑程序包):

java.lang.RuntimeException: Unable to start activity ComponentInfo{foo.bar.baz.id.MathleteIDActivity} ...
...
Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
...
Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment .fragments.TextFragment: make sure class name exists, is public, and has an empty constructor that is public
...
Caused by: java.lang.ClassNotFoundException: .fragments.TextFragment
...

My directory structure seems kosher: I've got the package directories foo/bar/baz/ inside src/main/java/ in the project root, and within that package:

fragments/
    TextFragment.java
id/
    NFCIDActivity.java
    MathleteIDActivity.java
    ...
...

MathleteIDActivity.java reads as follows:

package foo.bar.baz.id;

import android.widget.TextView;
import android.os.Bundle;
import foo.bar.baz.R;

public final class MathleteIDActivity extends NFCIDActivity {
    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        TextView textView = (TextView) findViewById(R.id.fragment_text);
        textView.setText("Scan mathlete tag");
    }
}


NFCIDActivity是扩展Android的Activity的抽象类。其唯一方法是覆盖onCreate

@Override
public void onCreate(Bundle savedState) {
    super.onCreate(savedState);
    setContentView(R.layout.id_activity);
}


TextFragment.java的内容如下:

package foo.bar.baz.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import foo.bar.baz.R;

public final class TextFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.text_fragment, container, false);
    }
}


src/main/res/layout/id_activity.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" >
    <fragment
        android:name=".fragments.TextFragment"
        android:id="@+id/id_message" >
    </fragment>
</LinearLayout>


在同一目录中,text_fragment.xml定义TextFragment的布局:

<?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" >
    <TextView
        android:id="@+id/fragment_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </TextView>
</LinearLayout>


我已经尝试过:


TextFragment添加一个公共的无参数构造函数。同样的错误。
通过其完全合格的程序包名称foo.bar.baz.fragments.TextFragment引用布局文件中的片段。同样的错误,现在命名新程序包。
TextFragment的源文件移动到顶级包foo.bar.baz并调整引用。同样的错误,现在命名新程序包。


有任何想法吗?

最佳答案

使用FragmentManager作为快速解决方案以编程方式实例化片段:

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

TextFragment tf = new TextFragment();
ft.replace(R.id.container, tf);
ft.commit();


至于您的方法,请改用规范的程序包名称,然后清理生成您的项目。然后注意错误消息(它不能是“相同的错误”,因为它至少应提及不同的程序包名称)。

10-07 15:52