简体   繁体   English

错误:(17,21)错误:找不到适合于add(int,ExampleItemFragment)的方法

[英]Error:(17, 21) error: no suitable method found for add(int, ExampleItemFragment)

I'm using Android Studio. 我正在使用Android Studio。 I'm trying to develop an app where one fragment holds a list, and tapping an item displays another fragment with details. 我正在尝试开发一个片段包含一个列表的应用程序,点击一个项目会显示另一个包含详细信息的片段。 I built the list, following the tutorial available at: https://www.airpair.com/android/list-fragment-android-studio 我按照以下网址提供的教程构建了列表: https//www.airpair.com/android/list-fragment-android-studio

I was planning on taking the code apart again after seeing how it ran. 我计划在看到代码如何运行后再次将其拆开。 However, when I tried to run it (at step 6, where the basic list should be functioning), I got the error: 但是,当我尝试运行它时(在第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)

in

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

Where & how should I implement this .add() method, or should I do something to the object I'm passing in order to make an inherited .add() accept it? 我应该在哪里和如何实现此.add()方法,或者应该对要传递的对象做些什么,以使继承的.add()接受它? The class ExampleItemFragment already extends Fragment. 类ExampleItemFragment已经扩展了Fragment。

ExampleItemFragment's code (mostly untouched from the default code): ExampleItemFragment的代码(大多数未更改默认代码):

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

}

You have to import the Fragment from the support library, not from the android.widget. 您必须从支持库而不是从android.widget导入Fragment。 Use import android.support.v4.app.Fragment; 使用import android.support.v4.app.Fragment; in your fragment. 在你的片段中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM