为什么FragmentTransaction无法通过它的ID找到布局区域?显示错误

时间:2015-02-22 22:54:59

标签: android android-layout android-activity android-fragments android-linearlayout

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

enter image description here 我的代码: 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>

日志中的错误:

enter image description here

3 个答案:

答案 0 :(得分:1)

除了我不了解你的StartFrag课程外,一切看起来都不错。它必须是Fragment的子类。 在onCreateView中,您应该为片段类膨胀xml,如: inflater.inflate(R.layout.start_frag ...

告诉我们。

答案 1 :(得分:1)

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

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

您当前的问题只是编译错误,我之前的回答仍然有效。

答案 2 :(得分:0)

请显示您的logcat。你究竟面临什么错误? 一个建议是你导入正确的Fragment类?

在这种情况下使用android.support.v4.app.FragmentManager时,片段类应该是android.support.v4.app.Fragment而不是android.app.Fragment。
请验证并发布您的logact错误。