FragmentTransaction.add()参数问题

时间:2015-11-15 03:55:45

标签: android add resolve

我希望在点击一个按钮后显示一个简单的片段...貌似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(扩展Fragment的类)...

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

在我的activity_home.xml中,我有一个linearlayout:

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

我认为这是我在主要活动中进口的一个问题,可能吗?

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);
    }

}

4 个答案:

答案 0 :(得分:0)

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

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

试试这个:

import android.support.v4.app.Fragment;

public class contact extends Fragment {
...
}

答案 1 :(得分:0)

在您的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;

答案 2 :(得分:0)

试试这个;

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

答案 3 :(得分:0)

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

自:     import android.app.Fragment; 至:     import android.support.v4.app.Fragment;

我希望这对你有用......