2016-05-12 50 views
-1

當我啓動我的應用程序我得到以下異常:如何實現void OnFragmentInteractionListener?

了java.lang.RuntimeException:com.demo.mtin.mtin.MainActivity必須實現OnFragmentInteractionListener

有人可以幫助我? 我需要在MainActivity中添加哪些代碼?

我有兩個空白片段:PopularFragment和RatedFragment

MainActivity波紋管:

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

請提供您的問題更好的信息。我推薦在[this]之前閱讀(http://developer.android.com/intl/es/training/basics/fragments/communicating.html) – UclidesGil

回答

0

這實際上是一個非常直接的錯誤信息,去這樣的:

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

    @Override 
    public void onFragmentInteraction(Uri uri) { 

    }} 

你必須指出你的活動實現了片段的接口,然後實現它(可以是void t霍夫)。

而且在你的片段如下:

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

我只能將它導入到一個片段中。如何做2個片段,以及如何創建onFragmentInteraction?謝謝:) –

+0

我會編輯我的答案。 – Amesys