【问题标题】:Error: incompatible types: android.support.v4.app.Fragment cannot be converted to android.app.Fragment错误:不兼容的类型:android.support.v4.app.Fragment 无法转换为 android.app.Fragment
【发布时间】:2019-03-06 10:34:11
【问题描述】:

在我问这个问题之前,我已经尝试了这里提供的所有可用选项和解决方案,但无济于事。学习使用此链接 https://abhiandroid.com/ui/fragment 创建片段。有两个片段类 除了 MainActivity。这两个片段类没有抛出错误,但是 MainActivity 在这一行做

fragmentTransaction.replace(R.id.frameLayout, fragment); 

在这个方法下。参数“fragment”用红色下划线标记

private void loadFragment(Fragment fragment) {
// create a FragmentManager
    FragmentManager fm = getFragmentManager();
// create a FragmentTransaction to begin the transaction and replace the 
//Fragment
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
// replace the FrameLayout with new Fragment
    fragmentTransaction.replace(R.id.frameLayout, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit(); // save the changes
}

错误消息说 第二个参数类型错误。发现需要 android.support.v4.app.Fragment android.app.Fragment

当我用鼠标悬停时,如果重建,我有这个

错误:(51, 55) 错误:不兼容的类型:android.support.v4.app.Fragment 无法转成android.app.Fragment

为了清楚起见,我添加了代码

MainActivity XML

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">
    <!-- display two Button's and a FrameLayout to replace the Fragment's  -->
    <Button
        android:id="@+id/firstFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/button_background_color"
        android:text="First Fragment"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <Button
        android:id="@+id/secondFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@color/button_background_color"
        android:text="Second Fragment"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp" />
</LinearLayout>

主活动

包 com.example.android.fragmentexample;

import android.support.v4.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button firstFragment, secondFragment;

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

        Fragment fragment = new Fragment();

// get the reference of Button's
        firstFragment = (Button) findViewById(R.id.firstFragment);
        secondFragment = (Button) findViewById(R.id.secondFragment);

// perform setOnClickListener event on First Button
        firstFragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// load First Fragment
                loadFragment(new FirstFragment());
            }
        });
// perform setOnClickListener event on Second Button
        secondFragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// load Second Fragment
                loadFragment(new SecondFragment());
            }
        });

    }

    private void loadFragment(Fragment fragment) {
// create a FragmentManager
        FragmentManager fm = getFragmentManager();
// create a FragmentTransaction to begin the transaction and replace the 
 //Fragment
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
// replace the FrameLayout with new Fragment
        fragmentTransaction.replace(R.id.frameLayout, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit(); // save the changes
    }
}

 FirstFragment xml

<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="com.example.android.fragmentexample.FirstFragment">

    <!-- TODO: Update blank fragment layout -->
   <RelativeLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <!--TextView and Button displayed in First Fragment -->
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerHorizontal="true"
           android:layout_marginTop="100dp"
           android:text="This is First Fragment"
           android:textColor="@color/black"
           android:textSize="25sp" />

       <Button
           android:id="@+id/firstButton"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_centerInParent="true"
           android:layout_marginLeft="20dp"
           android:layout_marginRight="20dp"
           android:background="@color/green"
           android:text="First Fragment"
           android:textColor="@color/white"
           android:textSize="20sp"
           android:textStyle="bold" />

   </RelativeLayout>

</FrameLayout>

FirstFragment.java

package com.example.android.fragmentexample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
/**
 * A simple {@link Fragment} subclass.
 */
public class FirstFragment extends Fragment {

    View view; Button firstButton;

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_first, container, false);
        //get the reference of the FirstButton
        firstButton = view.findViewById(R.id.firstButton);
        //perform setOnClickListener on the firstButton
        firstButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { 
                  //display a message by using a toast
                Toast.makeText(getActivity(), "First Fragment", 
                Toast.LENGTH_SHORT).show();


            }
        });

        return view;
    }

}

secondFragment 类和 xml 文件类似于 FirstFragment 类和 xml 文件,只是在必要时更改了名称以区别于其他

【问题讨论】:

标签: android android-fragments methods


【解决方案1】:
【解决方案2】:

在您的 MainActivity 中,

只需更改这两个导入

import android.app.FragmentManager;
import android.app.FragmentTransaction;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

现在可以正常使用了!!

【讨论】:

  • 你应该得到一些荣誉,谢谢一百万。无论如何,我使用了您的建议,然后从 getFragmentManager 更改为 getSupportFragmentManager,因为 @theanilpaudel 也做出了贡献。
【解决方案3】:

在你的 MainActivity 的 loadFragment() 中使用这个

FragmentManager fm = getSupportFragmentManager();

而不是

FragmentManager fm = getFragmentManager();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多