Java 错误:(17,21)错误:找不到适合添加的方法(int,ExampleItemFragment)

Java 错误:(17,21)错误:找不到适合添加的方法(int,ExampleItemFragment),java,android,android-fragments,Java,Android,Android Fragments,我正在使用Android Studio。我正在尝试开发一个应用程序,其中一个片段包含一个列表,点击一个项目将显示另一个包含详细信息的片段。我根据以下教程构建了列表: 在看到代码如何运行后,我打算再次将其拆开。但是,当我尝试运行它时(在步骤6,基本列表应该在其中运行),我得到了错误: Error:(17, 21) error: no suitable method found for add(int,ExampleItemFragment) method FragmentTransaction.a

我正在使用Android Studio。我正在尝试开发一个应用程序,其中一个片段包含一个列表,点击一个项目将显示另一个包含详细信息的片段。我根据以下教程构建了列表:

在看到代码如何运行后,我打算再次将其拆开。但是,当我尝试运行它时(在步骤6,基本列表应该在其中运行),我得到了错误:

Error:(17, 21) error: no suitable method found for add(int,ExampleItemFragment)
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; ExampleItemFragment cannot be converted to Fragment)

我应该在哪里实现这个.add()方法,或者我应该对传递的对象做些什么,以使继承的.add()接受它?ExampleItemFragment类已经扩展了Fragment

ExampleItemFragment的代码(与默认代码基本相同):

package com.example.brittany.fragmentlistexample;
导入android.app.Activity;
导入android.os.Bundle;
导入android.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AbsListView;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.ListAdapter;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.example.brittany.fragmentlistexample.dummy.DummContent;
导入java.util.ArrayList;
导入java.util.List;
公共类ExampleItemFragment扩展片段实现AbsListView.OnItemClickListener{
私有列表exampleListItemList;//在片段列表的顶部
//TODO:重命名参数参数,选择匹配的名称
//片段初始化参数,例如ARG_ITEM_NUMBER
私有静态最终字符串ARG_PARAM1=“PARAM1”;
私有静态最终字符串ARG_PARAM2=“PARAM2”;
//TODO:重命名和更改参数类型
私有字符串mParam1;
私有字符串mParam2;
私有OnFragmentInteractionListener mListener;
/**
*片段的ListView/GridView。
*/
私有AbsListView-mListView;
/**
*用于填充ListView/GridView的适配器
*观点。
*/
私有列表适配器mAdapter;
//TODO:重命名和更改参数类型
公共静态ExampleItemFragment newInstance(字符串param1,字符串param2){
ExampleItemFragment=新的ExampleItemFragment();
Bundle args=新Bundle();
args.putString(ARG_PARAM1,PARAM1);
args.putString(ARG_PARAM2,PARAM2);
fragment.setArguments(args);
返回片段;
}
/**
*片段管理器实例化
*碎片(如屏幕方向改变时)。
*/
public ExampleItemFragment(){
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
exampleListItemList=新建ArrayList();
添加(新的ExampleListItem(“示例1”));
添加(新的ExampleListItem(“示例2”));
添加(新的ExampleListItem(“示例3”));
mAdapter=新的ExampleListAdapter(getActivity(),exampleListItemList);
如果(getArguments()!=null){
mParam1=getArguments().getString(ARG_PARAM1);
mParam2=getArguments().getString(ARG_PARAM2);
}
//TODO:更改适配器以显示您的内容
mAdapter=newarrayadapter(getActivity(),
android.R.layout.simple_list_item_1,android.R.id.text1,DummyContent.ITEMS);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u示例项目,容器,错误);
//设置适配器
mListView=(AbsListView)view.findviewbyd(android.R.id.list);
((AdapterView)mListView).setAdapter(mAdapter);
//设置McClickListener,以便在单击项目时收到通知
mListView.setOnItemClickListener(此);
返回视图;
}
@凌驾
公共事务主任(活动){
超级转速计(活动);
试一试{
mListener=(OnFragmentInteractionListener)活动;
}catch(ClassCastException e){
抛出新的ClassCastException(activity.toString()
+“必须实现OnFragmentInteractionListener”);
}
}
@凌驾
公共无效连接(){
super.onDetach();
mListener=null;
}
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
ExampleListItem项目=(ExampleListItem)this.exampleListItemList.get(位置);
Toast.makeText(getActivity(),item.getItemTitle()+“已单击!”
,Toast.LENGTH_SHORT)show();
if(null!=mListener){
//通知活动回调接口(活动,如果
//片段被附加到一个)上,表示已选择项目。
onFragmentInteraction(DummyContent.ITEMS.get(position.id));
}
}
/**
*此片段的默认内容有一个文本视图,当
*列表为空。如果要更改文本,请调用此方法
*提供它应该使用的文本。
*/
public void setEmptyText(字符序列emptyText){
View-emptyView=mListView.getEmptyView();
如果(清空文本视图的视图实例){
((TextView)emptyView.setText(emptyText);
}
}
/**
*此接口必须由包含以下内容的活动实现
*片段,以允许通信此片段中的交互
*该活动以及其中可能包含的其他片段
*活动。
*

*有关更多信息,请参阅Android培训课程。 */ FragmentInteractionListener上的公共接口{ //TODO:更新参数类型和名称 公共void onFragmentInteraction(字符串id); } }


您必须从支持库导入片段,而不是从android.widget导入。使用
导入android.support.v4.app.Fragment在片段中。

使用import android.support.v4.app.fragment;而不是导入android.app.Fragment@哈里什:谢谢,这使它得以编译,但一旦在我的平板电脑上打开它,它立即停止工作。你知道我能去哪里吗
package com.example.brittany.fragmentlistexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new ExampleItemFragment())
                .commit();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}
package com.example.brittany.fragmentlistexample;

import android.app.Activity;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.brittany.fragmentlistexample.dummy.DummyContent;

import java.util.ArrayList;
import java.util.List;

public class ExampleItemFragment extends Fragment implements AbsListView.OnItemClickListener {
private List exampleListItemList; // at the top of your fragment list

// 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;

/**
 * The fragment's ListView/GridView.
 */
private AbsListView mListView;

/**
 * The Adapter which will be used to populate the ListView/GridView with
 * Views.
 */
private ListAdapter mAdapter;

// TODO: Rename and change types of parameters
public static ExampleItemFragment newInstance(String param1, String param2) {
    ExampleItemFragment fragment = new ExampleItemFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

/**
 * Mandatory empty constructor for the fragment manager to instantiate the
 * fragment (e.g. upon screen orientation changes).
 */
public ExampleItemFragment() {
}

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

    exampleListItemList = new ArrayList();
    exampleListItemList.add(new ExampleListItem("Example 1"));
    exampleListItemList.add(new ExampleListItem("Example 2"));
    exampleListItemList.add(new ExampleListItem("Example 3"));
    mAdapter = new ExampleListAdapter(getActivity(), exampleListItemList);

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

    // TODO: Change Adapter to display your content
    mAdapter = new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
            android.R.layout.simple_list_item_1, android.R.id.text1, DummyContent.ITEMS);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_exampleitem, container, false);

    // Set the adapter
    mListView = (AbsListView) view.findViewById(android.R.id.list);
    ((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);

    // Set OnItemClickListener so we can be notified on item clicks
    mListView.setOnItemClickListener(this);

    return view;
}

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

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    ExampleListItem item = (ExampleListItem)this.exampleListItemList.get(position);
    Toast.makeText(getActivity(), item.getItemTitle() + " Clicked!"
            , Toast.LENGTH_SHORT).show();
    if (null != mListener) {
        // Notify the active callbacks interface (the activity, if the
        // fragment is attached to one) that an item has been selected.
        mListener.onFragmentInteraction(DummyContent.ITEMS.get(position).id);
    }
}

/**
 * The default content for this Fragment has a TextView that is shown when
 * the list is empty. If you would like to change the text, call this method
 * to supply the text it should use.
 */
public void setEmptyText(CharSequence emptyText) {
    View emptyView = mListView.getEmptyView();

    if (emptyView instanceof TextView) {
        ((TextView) emptyView).setText(emptyText);
    }
}

/**
 * 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(String id);
}

}