Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法解析FragmentTransaction.add()方法。为什么我会犯这个错误?_Android_Android Fragments_Fragmenttransaction - Fatal编程技术网

Android 无法解析FragmentTransaction.add()方法。为什么我会犯这个错误?

Android 无法解析FragmentTransaction.add()方法。为什么我会犯这个错误?,android,android-fragments,fragmenttransaction,Android,Android Fragments,Fragmenttransaction,我已经看了其他答案,但没有一个对我有帮助。 我确保使用import android.support.v4.app.Fragment;所以这不是问题所在 MainActivity.java 错误: 为什么我会得到这个 在activity_main.xml中,我有一个框架布局 fragment.xml 改变这个 import android.app.Fragment; 到 在myFragment类中 此外,我还建议对您的片段使用适当的命名约定 不需要android:orientation=相对布局

我已经看了其他答案,但没有一个对我有帮助。 我确保使用import android.support.v4.app.Fragment;所以这不是问题所在

MainActivity.java

错误:

为什么我会得到这个

在activity_main.xml中,我有一个框架布局

fragment.xml

改变这个

import android.app.Fragment;

在myFragment类中

此外,我还建议对您的片段使用适当的命名约定


不需要android:orientation=相对布局中的垂直

粗心的错误。您应该调用v4.app.Fragment而不是app.Fragment

打开myFragment.java

不要

Error:(20, 28) error: no suitable method found for add(int,myFragment)
method FragmentTransaction.add(Fragment,String) is not applicable
(argument mismatch; int cannot be converted to Fragment)
method FragmentTransaction.add(int,Fragment) is not applicable
(argument mismatch; myFragment cannot be converted to Fragment)
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:id="@+id/fragmentContainer">

</FrameLayout>
package com.example.nirvan.fragmentsexample2;

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 myFragment extends Fragment
{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
 savedInstanceState)
{
    return inflater.inflate(R.layout.fragment,container,false);
}

public myFragment(){}
}
 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:paddingLeft="145dp"
    android:text="fragment One"/>

</RelativeLayout>
import android.app.Fragment;
import android.support.v4.app.Fragment;
import android.app.Fragment;
import android.support.v4.app.Fragment;