Java 使用片段库膨胀类片段时出错

Java 使用片段库膨胀类片段时出错,java,android,android-fragments,Java,Android,Android Fragments,我试图测试片段类,但不知何故,我的程序不断出现错误,导致类片段膨胀。我知道关于这个错误的问题已经被反复问过了,但我发现答案没有帮助。因为我不需要API

我试图测试片段类,但不知何故,我的程序不断出现错误,导致类片段膨胀。我知道关于这个错误的问题已经被反复问过了,但我发现答案没有帮助。因为我不需要API<11的支持,所以我使用的是android.app.Activity库和android.app.Fragment库,它有getFragmentManager()方法。我自己在代码中没有看到任何问题,所以有人能帮我吗

MainActivity.java

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

Fragment fragment1;
Button button1;

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

    fragment1 = new MainActivityFragment1();

    button1 = (Button) findViewById(R.id.button1);

    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getFragmentManager().beginTransaction()
                    .replace(R.id.fragment, fragment1)
                    .commit();
        }
    });
MainActivityFragment1.java

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class MainActivityFragment1 extends Fragment {

public MainActivityFragment1() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment1, container, false);
}
}
Fragment1.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1ST"
            android:onClick="onClick1"/>

    </LinearLayout>

    <fragment
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

您的主要活动不是扩展FragmentActivity或其任何子类。根据您的要求,尝试将其更改为FragmentActivity或AppCompatActivity

编辑:-在这种特殊情况下--

在活动布局文件中声明片段时,需要在标记中声明android:name属性

名称应该指向片段类的限定名称


此标记仅指定要在布局中实例化的片段类。因此,在您的情况下,标签丢失,因此碎片布局没有膨胀。

@syk:FYI

使用此
android.support.v4.app.Fragment
代替导入android.app.Fragment。为您的片段部分

并使用
FragmentActivity
代替
Activity

public class MainActivity extends FragmentActivity {
然后导入这个

android.support.v4.app.FragmentActivity

您应该发布日志cat+您的xml
ActionBarActivity
已被弃用。请问为什么我需要FragmentActivity而不是Activity或AppCompatActivity?FragmentActivity不是支持库的一部分吗?是的,你是对的。如果不使用支持库,则不需要FragmentActivity。我已经编辑了我的答案,您只需要添加指向您的片段类的Android:name。希望这能有所帮助。我意识到我在MainActivity.xml中使用了标记,但试图通过MainActivity.java源代码扩大片段。谢谢你们的帮助。我可能有点误解,但这不是旧平台的支持库吗?FragmentActivity是否用于兼容性,FragmentActivity是否用于较新的平台?在这里,他们没有使用碎片活动。
android.support.v4.app.FragmentActivity