FragmentTransaction.add() 参数问题

FragmentTransaction.add() parameters issues

我想在单击按钮后显示一个简单的片段...似乎 android 不想让我这样做,因为我针对此问题检查的每个问答和教程都没有解决任何问题所以我希望你们能帮助我,因为这个小错误让我抓狂:

        fab.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View view)
            {

                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                contact fragment = new contact();
                ft.add(R.id.MyFragment, fragment);
                ft.commit();

            }
        });

问题来自以下行: (无法解析方法 'add(int, org.zewde.futuristiclivewallpapers.contact)')即使我有 contact.class(这是 class 扩展片段)...

ft.add(R.id.MyFragment, fragment);

在我的 activity_home.xml 中,我有一个线性布局:

    <LinearLayout
        android:id="@+id/MyFragment"
        android:orientation="horizontal"
        android:layout_width="0dp"
        android:layout_height="fill_parent"/>

我认为这是我在主 activity 中导入的问题,也许吧?

import android.content.Intent;

import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;

import android.support.v4.app.Fragment;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import  android.support.v4.app.FragmentTransaction;

import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentPagerAdapter;

import android.support.v4.view.ViewPager;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

这是我的目录的屏幕截图: Screenshot


更新:这是我的 contact.class,我还没有添加任何代码...

package org.zewde.futuristiclivewallpapers;

import android.app.Activity;
import android.net.Uri;
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.
 * Activities that contain this fragment must implement the
 * {@link contact.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link contact#newInstance} factory method to
 * create an instance of this fragment.
 */
public class contact extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment contact.
     */
    // TODO: Rename and change types and number of parameters
    public static contact newInstance(String param1, String param2) {
        contact fragment = new contact();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

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

    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

}

您可能在联系人 class 中扩展了错误的片段类型。

由于您正在调用 getSupportFragmentManager(),因此您需要添加支持片段 (android.support.v4.app.Fragment) 而不是普通片段 (android.app.Fragment)。尝试扩展联系人 class 中的支持片段,看看是否能解决问题。

试试这个:

import android.support.v4.app.Fragment;

public class contact extends Fragment {
...
}

在您的 contact.java 中,替换以下导入:

package org.zewde.futuristiclivewallpapers;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

与以下一个:

package org.zewde.futuristiclivewallpapers;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment ;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

试试这个;

 FragmentManager fm = getSupportFragmentManager();
 fm.beginTransaction()
    .replace(R.id.MyFragment, new YourFragmentName())
    .commit();

我相信您导入了一个与 getSupportFragment class 不兼容的片段 class 版本。更改:

来自: 导入 android.app.Fragment; 到: 导入 android.support.v4.app.Fragment;

希望对你有用...