Adnxy Adnxy - 6 months ago 255
Java Question

How to implement void OnFragmentInteractionListener?

When I launch my app I get following exception:

java.lang.RuntimeException: com.demo.mtin.mtin.MainActivity must implement OnFragmentInteractionListener

Can someone help me with this?
What code I need to add in MainActivity and where?

I have two blank fragments: PopularFragment and RatedFragment

MainActivity bellow:

public class MainActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;
private MoviesAdapter mAdapter;


@Override
protected void onResume() {
SharedPreferences sharedPref = getBaseContext().getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
String order = sharedPref.getString("order", "popular");
if (order.equals("popular")) {
getPopularMovies();
} else {
getTopRatedMovies();
}
super.onResume();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// uci i postaviti order -> popular
Log.d("NESTO", "NESTO-onCreate");
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);



}
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PageAdapter adapter = new PageAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
});
}

@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) {

Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);


int id = item.getItemId();


if (id == R.id.action_settings) {


return true;
}

return super.onOptionsItemSelected(item);
}

Answer

This is actually a pretty straight-forward error message, go for this:

public class MainActivity extends AppCompatActivity implements PopularFragment.OnFragmentInteractionListener, RatedFragment.OnFragmentInteractionListener { ... 

    @Override
    public void onFragmentInteraction(Uri uri) {

    }}

You must indicate that your activity implement the fragment's interface, then implement it (can be void though).

And have the following in your fragments :

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