我正在尝试执行这些步骤(下面给出)来调用我的活动中的片段方法,而不使用任何片段ID和标记。但我得到NullpointerException。这对我来说非常重要...这些正确的步骤是否从活动中调用片段方法? 如果是,我做得对吗?如果没有,有没有更好的方法从我的活动中调用片段方法?和我的MainActivty和Fragmet在下面给出... plz help,thanks
_STEPS_WHICH_I_AM_TRYING_TO_DO
1步骤)首先创建一个界面
public interface MyInterface {
void myAction() ;
}
您的片段必须实现此接口。
public MyFragment extends Fragment implements MyInterface
2 STEP)在您的活动中,定义MyInterface类型的字段:
private MyInterface listener ;
public void setListener(MyInterface listener) {
this.listener = listener ;
}
3 STEP)创建片段并添加片段时:
setListener(myFragment);
最后,当你想要调用Fragment方法的condtion时,只需调用:
listener.myAction() ; // this will call the implementation in your MyFragment class
MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
private hell listener;
ClaimFragment claimFragment;
setListener(claimFragment);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
listener.willwork();
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) { }
@Override
public void onPageScrollStateChanged(int arg0) { }
});
}
tabLayout.setupWithViewPager(viewPager);
//THIS IS MY INTERFACE IN MAINACTIVITY
public interface hell {
void willwork();
}
public void setListener(hell listener) {
this.listener = listener ;
}
ClaimFragment.java
public class ClaimFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener , MainActivity.hell {
@Override
public void willwork() {
Toast.makeText(getContext(), "this is ClaimFrag", Toast.LENGTH_SHORT).show();
}
}
MY_LOG
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: FATAL EXCEPTION: main
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: Process: earn.free., PID: 20843
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke interface method 'void earn.free.cashback.snapcash.MainActivity$hell.willwork()' on a null object reference
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at earn.free.cashback.snapcash.MainActivity$2.onPageSelected(MainActivity.java:152)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1856)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:625)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:609)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:551)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSele cted(TabLayout.java:2008)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1025)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at android.support.design.widget.TabLayout.selectTab(TabLayout.java:995)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1272)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1377 )
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19866)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5254)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-06 18:31:23.104 20843-20843/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-06 18:31:23.105 747-1255/? W/ActivityManager: Force finishing activity 1 earn.free.cashback.snapcash/.MainActivity