OnFragmentInteractionListener를 구현하는 방법

android studio 0.8.2에서 탐색 창이있는 마법사 생성 앱이 있습니다.

조각을 만들고 newInstance ()로 추가 했는데이 오류가 발생합니다.

com.domain.myapp E / AndroidRuntime ﹕ 치명적 예외 : 기본 java.lang.ClassCastException : com.domain.myapp.MainActivity@422fb8f0은 OnFragmentInteractionListener를 구현해야합니다.

이 OnFragmentInteractionListener를 구현하는 방법을 찾을 수 없습니다 ?? 안드로이드 SDK 문서에서도 찾을 수 없습니다!

MainActivity.java

import android.app.Activity;

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
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.support.v4.widget.DrawerLayout;


public class MainActivity extends Activity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
 * Used to store the last screen title. For use in {@link #restoreActionBar()}.
 */
private CharSequence mTitle;

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

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();

    switch (position) {
        case 0: fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                .commit(); break;
        case 1: fragmentManager.beginTransaction()
                .replace(R.id.container, AboutFragment.newInstance("test1", "test2"))
                .commit(); break; // this crashes the app
        case 2: fragmentManager.beginTransaction()
                .replace(R.id.container, BrowseQuotesFragment.newInstance("test1", "test2"))
                .commit(); break; // this crashes the app
    }
}


public void onSectionAttached(int number) {
    switch (number) {
        case 1:
            mTitle = getString(R.string.title_section1);
            break;
        case 2:
            mTitle = getString(R.string.title_section2);
            break;
        case 3:
            mTitle = getString(R.string.title_section3);
            break;
    }
}

public void restoreActionBar() {
    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.main, menu);
        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

@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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

}



답변

여기에 게시 된 답변은 도움이되지 않았지만 다음 링크는 도움이되었습니다.

http://developer.android.com/training/basics/fragments/communicating.html

인터페이스 정의

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;

    // Container Activity must implement this interface
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    ...
}

예를 들어, 조각에서 다음 메소드는 사용자가 목록 항목을 클릭 할 때 호출됩니다. 프래그먼트는 콜백 인터페이스를 사용하여 이벤트를 상위 활동에 전달합니다.

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // Send the event to the host activity
    mCallback.onArticleSelected(position);
}

인터페이스 구현

예를 들어 다음 활동은 위 예제의 인터페이스를 구현합니다.

public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{
    ...

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Do something here to display that article
    }
}

API 23 업데이트 : 2015 년 8 월 31 일

재정의 된 메소드 onAttach(Activity activity)는 이제에서 더 이상 사용되지 않습니다 android.app.Fragment. 코드를로 업그레이드해야합니다.onAttach(Context context)

@Override
public void onAttach(Context context) {
    super.onAttach(context);
}


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

답변

@meda 답변을 읽은 후에도 여전히 이해하지 못하는 사람들을 위해이 문제에 대한 간결하고 완전한 설명이 있습니다.

하자 당신이 두 조각이 말을 Fragment_A하고 Fragment_B있는 자동 생성 된 응용 프로그램에서 있습니다. 생성 된 프래그먼트의 맨 아래에 다음 코드가 있습니다.

public class Fragment_A extends Fragment {

    //rest of the code is omitted

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }
}

public class Fragment_B extends Fragment {

    //rest of the code is omitted

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }
}

이 문제를 극복하려면 onFragmentInteraction활동 에 메소드 를 추가해야합니다 MainActivity2. 그 후에 implements는 다음 MainActivity과 같은 모든 조각 이 필요합니다 .

public class MainActivity2 extends ActionBarActivity
        implements Fragment_A.OnFragmentInteractionListener,
                   Fragment_B.OnFragmentInteractionListener,
                   NavigationDrawerFragment.NavigationDrawerCallbacks {
    //rest code is omitted

    @Override
    public void onFragmentInteraction(Uri uri){
        //you can leave it empty
    }
}

추신 : 간단히 말해서이 방법은 조각 간 통신에 사용될 수 있습니다. 이 방법에 대해 더 알고 싶은 분은이 링크 를 참조하십시오 .


답변

FragmentAndroid Studio에서 생성 한 자동 생성을 참조하십시오 . 새로운을 만들 때 FragmentStudio는 많은 코드를 스텁했습니다. 자동 생성 된 템플릿의 맨 아래에는 내부 인터페이스 정의가 OnFragmentInteractionListener있습니다. 귀하의 Activity요구는이 인터페이스를 구현합니다. 이것은 이벤트 Fragment를 알리기 위해 권장되는 패턴 Activity이므로 다른로드와 같은 적절한 조치를 취할 수 있습니다 Fragment. 세부 사항은이 페이지를 참조하고 “활동에 대한 이벤트 콜백 작성”섹션을 찾으십시오. http://developer.android.com/guide/components/fragments.html


답변

이 페이지를 방문하는 사람들에게는이 오류에 대한 추가 설명이 필요합니다. 제 경우에는 조각을 호출하는 활동 이이 경우 2 개의 구현을 필요로했습니다.

public class MyActivity extends Activity implements
    MyFragment.OnFragmentInteractionListener,
    NavigationDrawerFragment.NaviationDrawerCallbacks {
    ...// rest of the code
}

답변

조각에서 다음 코드를 제거해야합니다.

    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }

인터페이스 / 리스너는 활동 및 프래그먼트가보다 쉽게 ​​통신 할 수 있도록 기본적으로 작성됩니다.


답변

@ user26409021의 답변 외에도 ItemFragment를 추가 한 경우 ItemFragment의 메시지는 다음과 같습니다.

Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener} interface.

그리고 당신은 당신의 활동을 추가해야합니다;

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener, ItemFragment.OnListFragmentInteractionListener {

//the code is omitted

 public void onListFragmentInteraction(DummyContent.DummyItem uri){
    //you can leave it empty
}

더미 아이템은 ItemFragment의 맨 아래에있는 것입니다.


답변

나와 함께이 코드를 삭제했습니다.

@Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

다음과 같이 끝납니다 :

@Override
public void onAttach(Context context) {
    super.onAttach(context);
}