java - fragment 管理器事务参数类型错误

标签 java android android-fragments navigation-drawer

我正在开发一个抽屉导航,有两个不同的 fragment 。不起作用的是 fragmentManager.beginTransaction().replace(R.id.fragment,first).commit( ); 以及另一行中的以下 java 文件中的 。它引发的错误是:

Wrong 2nd argument type. Found: 'com.example.name_of_app.First', required: 'android.support.v4.app.Fragment' 

replace
(int,
android.support.v4.app.Fragment)
in FragmentTransaction cannot be applied
to
(int,
com.example.name_of_app.First)
 

MainActivity.java 文件中引发问题的部分:

public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            setTitle("First Fragment");
            First first = new First();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.fragment, first).commit();
        } else if (id == R.id.nav_gallery) {
            setTitle("Second Fragment");
            Second second = new Second();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.fragment, second).commit();

        } 

这是 First.java fragment

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


/**
 * A simple {@link Fragment} subclass.
 */
public class First extends Fragment {


    public First() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false);
    }

}

还有fragment_first.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context="First">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="50dp"

        android:text="@string/hello_blank_fragment" />

</FrameLayout>

最佳答案

如果您查看错误,您将看到 android.support.v4.app.Fragment。但是,您的类 First 具有 import android.app.Fragment; 导入。

它们不匹配。将导入更改为 import android.support.v4.app.Fragment;

关于java - fragment 管理器事务参数类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52063779/

相关文章:

android - 我可以通过在它们之间切换来使用两个 Button 的一个定义吗?

java - 将 DFDL 转换为 XML

java - 有没有可能在Tomcat服务器上运行一切正常,然后部署到互联网上就出问题了

java - 在 junit 中使用@rule 检查错误代码

android - 如何检查 GridView 到达屏幕底部?我想在它到达底部时显示更多图片

android - 如何在单击时在单独的警报对话框中缩放 map fragment

java - 并行执行功能文件会导致控制台上打印出空指针异常

android - 长按 Material Design 按钮允许粘贴到按钮标签中

java - Android - 将数据从父 fragment 传递到子 fragment

Android 应用程序在显示 android.widget.MediaController 时无法识别音量键按下