Android原生:E/AndroidRuntime: FATAL EXCEPTION: main

2019-07-12

有一个MainActivity包含:AFragment、BFragment。但是切换BFragment时,得到以下错误:

com.exampl E/AndroidRuntime: FATAL EXCEPTION: main
     Process: com.example, PID: 12845
     java.lang.RuntimeException: com.example.MainActivity@f3030ac must implement OnFragmentInteractionListener
     at com.example.BFragment.onAttach(ProfileFragment.java:84)

解决:

日志已经给了我们信息也就是说我们的BFragment也需要implement OnFragmentInteractionListener
所以将

public class MainActivity extends AppCompatActivity implements AFragment.OnFragmentInteractionListener{

改为:(也就是在implements添加 BFragment.OnFragmentInteractionListener

public class MainActivity extends AppCompatActivity implements AFragment.OnFragmentInteractionListener,BFragment.OnFragmentInteractionListener{
@2020  TCODE    津ICP备13002520号-4