在我的片段中,我有他的听众代码
private FragmentTwoCallBackListener fragmentTwoCallBackListener;
public interface FragmentTwoCallBackListener {
void onItemTypeClick(String itemType,String textInPunjabi);
}
public void onAdapterItemTypeClick(String textA, String textB){
if(fragmentTwoCallBackListener == null)
Log.w(TAG," fragmentTwoCallBackListener Null Error - ");
else
Log.w(TAG," fragmentTwoCallBackListener not Null - " );
fragmentTwoCallBackListener.onItemTypeClick(textInEnglish,textInPunjabi);
}
在正常使用情况下,该应用程序运行良好。但是当应用程序恢复时我收到了这个错误
java.lang.NullPointerException:在 com.shayari4u.punjabistatus.FragmentTwo.onAdapterItemTypeClick (FragmentTwo.java:133) 在 com.shayari4u.punjabistatus.FragmentTwoAdapter$1.onClick (FragmentTwoAdapter.java:115)
经过多次 Hit & Try 我发现在 App Resume 上fragmentTwoCallBackListener对象为空
我在 TabbedLayout 中使用这个片段。
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
f1 = new FragmentOne();
if (getIntent().hasExtra("type")) { // PUT EXTRAS FOR FRAGMENT IF GET NOTIFICATION
Bundle bundle = new Bundle();
bundle.putString("post_type", getIntent().getStringExtra("type"));
f1.setArguments(bundle);
Log.w(TAG,"Tabbed Activety Get Notification for Post Type - " + getIntent().getStringExtra("type"));
}
showItemFragment = new FragmentTwo();
showItemFragment.setFragmentTwoCallBackListener(this);
adapter.addFragment(showItemFragment, "Category");
adapter.addFragment(f1, "Posts");
if(adapter==null){
Log.w("Tabbed","Adapter is blank");
}{
viewPager.setAdapter(adapter);
}
}
如何解决这个问题请帮忙