为什么FragmentTransaction 无法通过ID 找到Layout 区域?显示错误

Why FragmentTransaction can't find the Layout area by it's ID? Showing error

我正在练习Fragment。该应用程序具有三个按钮 - Start、Button 1、Button 2。 Start 按钮应该启动片段,Button 1 按钮 2 显示相应的文本。 问题出在我的 MianActivity.java 文件中。 第 34 行显示错误:

我的代码: MainActivity.java:

 package com.mycompany.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
//import android.support.v4.app.*;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends FragmentActivity {
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
   android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
     StartFrag startFrag = new StartFrag();
  fragmentTransaction.add(R.id.textViewArea_1,startFrag);
        fragmentTransaction.commit();
   }
public void clickButton(View view) {

         Fragment newFragment;
    if (view == findViewById(R.id.button)) {
        newFragment=new Fragment();
 } else if (view == findViewById(R.id.button1)) {
        newFragment= new Frag1();//shows error: "Incompitable types Required: android.support.v4.app.Fragment, Found: com.mycompany.fragment.Frag1"
    } else if (view == findViewById(R.id.button2)) {
        newFragment= new Frag2();//shows error: "Incompitable types Required: android.support.v4.app.Fragment Found: com.mycompany.fragment.Frag2"
    } else {
 newFragment=new Fragment();
    }
   android.support.v4.app.FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.textViewArea_1,newFragment);//Line 34
        transaction.addToBackStack(null);
        transaction.commit();
 }

}

activity_main.xml:

    <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:background="#468499">

    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="Start"
        android:id="@+id/button"
        android:background="#6d0e0e"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="32dp"
        android:textColor="#147907"
        android:textStyle="bold"
        android:onClick="clickButton"/>

    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="Fragment 1"
        android:id="@+id/button1"
        android:layout_alignTop="@+id/button"
        android:layout_centerHorizontal="true"
        android:background="#6d0e0e"
        android:textColor="#147907"
        android:textStyle="bold"
        android:onClick="clickButton"/>

    <Button
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="Fragment 2"
        android:id="@+id/button2"
        android:layout_alignTop="@+id/button1"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="#6d0e0e"
        android:textColor="#147907"
        android:textStyle="bold"
        android:onClick="clickButton"/>


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/textViewArea_1"
        android:background="#fbf896"></LinearLayout>


</RelativeLayout>

StartFrag.java:

    package com.mycompany.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import android.support.v4.app.*;

public class StartFrag extends Fragment{

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_main,container,false);
    }
}

start_frag.xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="#468499">

</RelativeLayout>

Frag1.java:

    package com.mycompany.fragment;

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

public class Frag1 extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_1,container,false);
    }
}

frag_1.xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="#468499">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:text="Fragment 1"
        android:id="@+id/textView1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textColor="#6d0e0e"
        android:textSize="40dp" />
</RelativeLayout>

Frag2.java:

    package com.mycompany.fragment;

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

public class Frag2 extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_2,container,false);
    }
}

frag_2.xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="#468499">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:text="Fragment 2"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textColor="#6d0e0e"
        android:textSize="40dp" />

</RelativeLayout>

日志中的错误:

除了我不知道你的 StartFrag class 外,一切看起来都很好。它需要是 Fragment 的子 class。 在 onCreateView 中,您应该为片段 class 膨胀 xml,例如: inflater.inflate(R.layout.start_frag...

让我们知道。

请出示您的logcat。你到底面临什么错误? 一个建议是您导入正确的 Fragment class?

当你使用 android.support.v4.app.FragmentManager 时,片段 class 应该是 android.support.v4.app.Fragment 而不是 android.app.Fragment .
请验证它和 post 你的 logact 错误。

在您的 StartFrag 代码文件中,添加 import android.support.v4.app.Fragment; 。这是为了确保您与 getSupportFragmentManager 方法调用保持一致。你必须删除 import android.Fragment,假设你有它。 其他 Java 文件中存在相同的编译器问题。他们需要 import android.support.v4.app.Fragment.

注意:当您使用 Studio Wizard 时,它会插入 import android.Fragment 以与 Android 5 (Lollipop) 兼容。但是 pre-Android 5 与 Fragments 存在兼容性问题。因此,您必须执行这些 support.v4 导入,并手动更改它们。

你现在的问题只是一个编译错误,我之前的回答我认为仍然有效。